I came across this post whilst reading up on the correct ways for cleaning up event listeners attached to $scope in angularjs.
I am listening to an event on $rootScope in my app module run block:
app.run(['$rootScope', function($rootScope){
$rootScope.$on('$stateChangeStart', function(event, toState, toParams){
//do stuff here...
});
}]);
Since $scope is not available in the run block, can I listen for the destroy event on $rootScope instead?
So:
app.run(['$rootScope', function($rootScope){
var listenerFn = $rootScope.$on('$stateChangeStart', function(event, toState, toParams){
//do stuff here...
});
$rootScope.$on('$destroy', listenerFn);
}]);
I'm not so sure I can do it this way looking around various posts and docs online since it doesn't look like $rootScope has a destroy event. So what's the correct approach here?