2016-06-07 19 views
2

我有$ 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'); 
      } 

     }); 

    }]); 

任何想法如何實現這個。

在此先感謝

+0

可能的重複http://stackoverflow.com/questions/10486769/cannot-get-to-rootscope#10489658 – RamblinRose

+0

那麼有什麼問題? –

+0

運行塊在配置塊後執行。 – Revive

回答

2

.config.run之前exectuted,試試這個run and config order

+0

謝謝@Itsik Mauyhas,問題是按照調用的順序,它現在起作用。 – James