AngularJs broadcast/emit repeating execution too many times

504 views
Skip to first unread message

RB

unread,
Jul 11, 2014, 7:33:48 PM7/11/14
to ang...@googlegroups.com

nside one of the directive I have this

myApplication.directive('direc', ['$compile', '$http', '$templateCache', 'navigationService', function($compile, $http, $templateCache, navigationService) {    
scope.getScope = function(name) {
                console.log("in passScope method");
                navigationService.broadcast(name, scope.content,scope.headerLabel);               
            };

}

And I have one Service which have brodcast method

myApplication.factory('navigationService', function($rootScope) {
    var navigationService = {};
        navigationService.broadcast = function(name, object,headerLabel) {
            navigationService.object = object;      
            if(object.id)
            {
                navigationService.selectedElementId = object.id;
            }       
            $rootScope.$emit(name);
        };
        return navigationService;
});

And I have two controller which contains $rootScope.$on method.

myApplication.controller('childController', ['$scope', '$http', 'navigationService','$rootScope',
                                             function ($scope, $http, navigationService,$rootScope) {

    var cleanUpFunc = $rootScope.$on('leftSideClick', function() {
        // some logic.....
}
});

This controller is a child controller of the above controller.

myApplication.controller('subChildController', ['$scope', '$http', 'navigationService','$rootScope',
                                             function ($scope, $http, navigationService,$rootScope) {

    var cleanUpFunc = $rootScope.$on('tabDisplay', function() {
        // some logic.....
}
});

And i have two more controller one is root controller and one is left controller,root controller is a parent controller for all the controllers.

Now, this is a single-page app and page contains 10 links.I am calling function getScope('tabDisplay') on click event of a link , subChildController $on function executed once and when i click on some other link which have getScope('tabDisplay').$on function will executed twice.... If I do it again, it happens three times and so on. What am I doing wrong here? I got the solution from this linkĀ AngularJs broadcast repeating execution too many times

but it is not wokring in my case.Please Advice.

Reply all
Reply to author
Forward
0 new messages