AngularJs broadcast repeating execution too many times

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

Inside one of my Angular controllers, I have this:

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

5条回答
  •  旧时难觅i
    2020-12-07 18:14

    If it helps anyone I had a similar issue in a directive. Each time the directive opened the number of times the event was fired increased.

    I solved it by using the following (this was in my controllers init function but I guess it could have been defined in the controller itself. In my case my controller needed to reinitialise when the event fired)

     if (!$scope.onMyEvent) $scope.onMyEvent= $scope.$on('myEvent',function(event,data){
            .....
            });
    

提交回复
热议问题