Answer the question
In order to leave comments, you need to log in
How to remove $rootScope.$on listener in Angularjs?
Greetings.
Listening to the event for $rootScope in the directive. An element with a directive can disappear and reappear on the page. Each time the visibility changes, a listener for $rootScope is added. As a result, when the event is raised, the handler is called multiple times.
What is the correct way to remove $rootScope.$on in Angularjs? Now I'm using $rootScope.$$listener, but the documentation says that you shouldn't use private fields.
Answer the question
In order to leave comments, you need to log in
The moment of deleting an element is tracked as follows:
var deregFunc = $rootScope.$on('someEvent', function() {});
scope.$on('$destroy', function() {
deregFunc();
});
https://docs.angularjs.org/api/ng/type/$rootScope.Scope
$on
...
Returns
function()
Returns a deregistration function for this listener.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question