0

So I have this situation where one controller is emitting the event and the other controller has the listener. Here is the code:

In controller A, I have this method:

$scope.process = function () {
    var taskName = 'process';
    $scope.$emit('process', taskName);
}

In controller B, I have this:

$rootScope.$on('process', function (event, taskName) {
    //Do something here
});

Now whenever I visit other pages on application and comeback to this, the process listener gets created twice. I cannot use controller scope as the event is getting emitted from other controller. How can I destroy listener once it has completed its task? I have also tried $scope.$destroy(). Doesn't really work. What is the correct way of doing this?

I am on Angularjs 1.4.7.

1 Answer 1

1

Usually you do it in different way:

$rootScope.$broadcast(...)
...
$scope.$on(...)

Then you do not need to unsubscribe. If you really need for some reason to subscribe to $rootScope, then:

var deregister = $scope.$on(...);
...
deregister(); // destory that listener
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.