$rootScope in onExit


I want to use $rootScope.$broadcast() inside onExit when state changes.

But for this, I need to inject $rootScope in the .config(), which is not possible in angular

 onExit: function () {
                //onExit is executed when we leave that state and go to another

Can someone help me to achieve it?


Use the $injector to inject the $rootScope into the onExit, like so

onExit: function($injector) {
    var rootScope = $injector.get('$rootScope');
    console.log("onExit: ", rootScope);

However, this should also work

onExit: function($rootScope) {
    console.log("$rootScope: ", $rootScope)

Here is a working plunk

Answered By – svarog

Answer Checked By – Marilyn (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.