8

In one controller I do :

$rootScope.$emit("newAction", {});

in another controller I do :

$rootScope.$on('newAction', function() {
          vm.newAction (...);
          vm.newAction (...);
          vm.newAction (...);
          vm.newAction (...);
          vm.newAction (...);
          vm.newAction (...);
      });

My problem is that $rootScope.$on is called multiple times. I don't know why.

If anybody has a hint... Thanks

user1260928
  • 3,269
  • 9
  • 59
  • 105

1 Answers1

18

$rootScope listener are not destroyed automatically. You need to destroy it using $destroy.

var customeEventListener = $rootScope.$on('newAction', function() {
          vm.newAction (...);
          vm.newAction (...);
          vm.newAction (...);
          vm.newAction (...);
          vm.newAction (...);
          vm.newAction (...);
      });

$scope.$on('$destroy', function() {
        customeEventListener();
  });

Refer this link
Working with $scope.$emit and $scope.$on

Community
  • 1
  • 1
Ved
  • 11,837
  • 5
  • 42
  • 60