$ when calling only one controller

I have controller

one that has an event named changeSafe

. I want other controllers to listen when this event is fired

secure.controller('wrapperCtrl', function ($scope, $rootScope, storeFactory, safeFactory) {    
    $scope.changeSafe = function (safeId) {
        $scope.loading = true;
        safeFactory.setSafe(safeId).then(function (safeData) {
            $scope.safe = safeData;
            $scope.loading = false;
            $rootScope.$broadcast('changeSafe', safeData);
        });
    }
});

      

The first page to load is called dashboard

when I add what is below the page, re-draws with $ scope.safe as I would expect.

secure.controller('dashboardCtrl', function ($scope, $rootScope, storeFactory, safeFactory) {    
    $scope.$on('changeSafe', function (event, arg) {        
        bindSafe(arg.safeId);
    }); 
}); 

      

I have almost the same on my history controller

secure.controller('historyCtrl', function($scope, $rootScope, storeFactory, safeFactory) {
    $scope.$on('changeSafe', function (event, arg) {        
       bindHistory(arg.safeId);
    }); 
});

      

Here I am in the section config

secure.config(['$routeProvider', function ($routeProvider) {
    $routeProvider
        .when('/history', {
            templateUrl: '/Angular/History/history.html',
            controller: 'historyCtrl'
        })
    .otherwise({
        templateUrl: '/Angular/Dashboard/dashboard.html',
        controller: 'dashboardCtrl'
    });
}]);

      

Whenever I press a button held within wrapperCtrl

, only $scope.$on

fires in dashboardCtrl

. Can anyone figure out why it $scope.$on

doesn't start from within the controller historyCtrl

? I also don't understand why it is called from dashboardCtrl

when I am no longer in that view.

When I step through the code, I actually see it being $rootScope.$on('changeSafe')

called multiple times on the story page and dashboard. I can't figure out why it is changing views to the toolbar though

+3


source to share


1 answer


I'm not sure if I have a complete understanding of your problem, but as far as I understand, you have a download order problem and this event is broadcast before the subscription has been initialized.


First of all, to save a lot of headaches, it is important to have a solid understanding of Event Handling in AnguarJS :

  • know the difference between subscribing to $ rootScope. $ on vs $ Sphere. $ On
  • know the difference between posting with $ broadcast vs $ emit on $ rootScope vs $ scope


From the comments it sounds like you used $ rootScope. $ on, which is not cleared when your controllers are destroyed (same as directives):

var changeSafeListnerUnbindFunction = $rootScope.$on('changeSafe', funciton() { //... });

$scope.$on('$destroy', changeSafeListnerUnbindFunction);

      

Given your use case, registering listeners in child scopes will capture events posted from $ rootScope. $ broadcast (which binds the top level to each child scope). You probably have a download problem?

+1


source







All Articles