Merge controllers

Asked

Viewed 44 times

1

I have this controller that sends a normal Error or Success notification

 $scope.add = function () {
        if(!$scope.name || !$scope.url ){
            $scope.success = null;
            return $scope.error = 'Preencha todos os campos.'
        }else{

            var newGroup = {
                name: $scope.name,
                url: $scope.url
            }

            $http.post("/api/group/add", newGroup).then(function (response) {
                if(response.data.err){
                    $scope.error = response.data.err;
                    $scope.success = null;
                }else{
                    getGroupByAccess();
                    clearForm();
                    userSessionService.updateUserMenu();
                    $scope.success = response.data.msg;
                    $scope.error = null;
                }
            });
        }
    }

And I have this controller that sends a more organized notification

var notifyApp = angular.module('notifyApp',['angular-growl','ngAnimate']);

notifyApp.controller('notifyCtrl',['$scope','growl',function($scope,growl){
    $scope.showError = function(){
        growl.error('This is a error mesage.',{title: 'Error!'});
    }
    $scope.showSuccess = function(){
        growl.success('This is a success mesage.',{title: 'Success!'});
    }
}]);

And this here is the call

.content(ng-controller="groupsController")
    .alert.alert-danger(ng-show='error' class='ng-hide')
      span{{error}}
        button.close(type='button', aria-label='Close' data-toggle='tooltip' data-placement='top' title='Close' ng-click='close()') x
    .alert.alert-success(ng-show='success' class='ng-hide')
      span{{success}}
        button.close(type='button', aria-label='Close' data-toggle='tooltip' data-placement='top' title='Close' ng-click='close()') x

How can I use this second controller in the first, remembering that everything is important and working, I just need to understand how it would be done and called

  • Create a service to store all your notification processes. This will allow you to keep code cleaner and extensible.

1 answer

1

In the first controller setting you can inject the second controller:

notifyApp.controller('primeiroCtrl', ['$scope', 'notifyCtrl', function($scope, notifyCtrl) {

(did not come the first controller creation code in your question)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.