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
.
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