如何在配置函数-角度内调用$rootScope变量

时间:2022-03-09 13:34:06

i have a value inside $rootscope and i want to access this value inside the config function , this is my code :

我在$rootscope中有一个值,我想在配置函数中访问这个值,这是我的代码:

--run function 
myApp.run(function ($rootScope) {

$rootScope.myVariable = "my value";

});


--config function



myApp.config(['$provide','$routeProvider','$stateProvider','$urlRouterProvider',function($provide,$routeProvider,$stateProvider,$urlRouterProvider) {

    // here ,i want to access to myVariable value to do some tests
        $urlRouterProvider.otherwise(function($injector){

            var $state = $injector.get('$state');
            var $rootScope = $injector.get('$rootScope');

               alert($rootScope.myVariable == "1");

            if ($rootScope.myVariable) {
                $state.go('mystat1'); 
            }
            else {
                 $state.go('mystat2');   
            }

        });

     }]);

any idea how to achieve this.

有什么办法可以实现的吗?

thanks in advance

谢谢提前

1 个解决方案

#1


2  

The .config block is exectuted before .run, try this run and config order.

.config块在.run之前执行,请尝试此运行和配置顺序。

#1


2  

The .config block is exectuted before .run, try this run and config order.

.config块在.run之前执行,请尝试此运行和配置顺序。