$scope.$on Called several times

Issue

I am using

$rootScope.$broadcast("closeviewMultiUserMainDetail");

and using in by below

$rootScope.$on("closeviewMultiUserMainDetail", function (event) {
            //other code
        });

sometimes ‍‍$rootScope.$on Called several times.

How can this be prevented?

Solution

$rootScope event listeners are not destroyed automatically and are always listening. That is why $rootScope.$on is getting called several times. You need to destroy the event listener using $destroy.

You can define your event like this:

var closeviewMultiUserMainDetail = $rootScope.$on(‘closeviewMultiUserMainDetail’, function(event) { });

You can then destroy it like this:

$scope.$on(‘$destroy’, function() {
closeviewMultiUserMainDetail(); });

Answered By – immortal

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published