AngularJs broadcast repeating execution too many times

后端 未结 5 1629
北恋
北恋 2020-12-07 17:41

Inside one of my Angular controllers, I have this:

// controller A
$rootScope.$on(\"myEventFire\", function(event, reload) {
    someAction();
});

5条回答
  •  甜味超标
    2020-12-07 18:23

    you can also remove it when the scope is destroyed, by running the return callback from the $rootScope.$on().

    I.e.

    var destroyFoo;
    
    destroyFoo = $rootScope.$on('foo', function() {});
    
    $scope.$on('$destroy', function() {
      destroyFoo(); // remove listener.
    });       
    

提交回复
热议问题