How to unsubscribe from a broadcast event in the application module launch block

I came across this after reading the correct ways to clear $ scope event listeners in angularjs.

I listen to the event on $ rootScope in the application module launch block:

app.run(['$rootScope', function($rootScope){
  $rootScope.$on('$stateChangeStart', function(event, toState, toParams){
    //do stuff here...
  });
}]);

Since $ scope is not available in the launch 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 sure I can do this by looking at various messages and documents on the Internet, since it does not look like $ rootScope has a destroy event. So what is the right approach here?

+4
source share

Source: https://habr.com/ru/post/1617704/


All Articles