LazioTibijczyk
LazioTibijczyk

Reputation: 1947

$rootScope.$on called multiple times

I'm using $rootScope.$emit('driver-loader'); once my webservice finished getting data. This is the only place 'driver-loader' gets emitted. Once I do...

var watchDriverLoader = $rootScope.$on('driver-loaded', function(){
        console.log('loaded');
        watchDriverLoader = null;
    });

...inside my controller, that console.log gets called multiple times, sometimes even 4-5 times.

Upvotes: 1

Views: 966

Answers (1)

sahil0021
sahil0021

Reputation: 77

You need to destroy $rootScope.$on manually.

var watchDriverLoader = $rootScope.$on('driver-loaded', function(){
    console.log('loaded');
});
$scope.$on('$destroy', watchDriverLoader);

Upvotes: 3

Related Questions