angularjsangular-ui-routerangularjs-scoperootscope

$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
                $rootScope.$broadcast('broadCastCloseModalStr');
            }

Can someone help me to achieve it?


Solution

  • 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