首页 > 解决方案 > $rootScope 在 onExit

问题描述

我想在状态改变时使用$rootScope.$broadcast()inside 。onExit

但为此,我需要注入,这在 Angular$rootScope.config()是不可能的

 onExit: function () {
                //onExit is executed when we leave that state and go to another
                $rootScope.$broadcast('broadCastCloseModalStr');
            }

有人可以帮我实现它吗?

标签: angularjsangular-ui-routerangularjs-scoperootscope

解决方案


使用 将$injector注入$rootScope到 onExit 中,就像这样

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

但是,这也应该有效

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

这是一个工作的笨蛋


推荐阅读