2

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

--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

6
  • Possible duplicate of stackoverflow.com/questions/10486769/… Commented Jun 7, 2016 at 11:35
  • So what's the problem? Commented Jun 7, 2016 at 11:36
  • Run blocks are executed after config blocks. You can not pass from .run to .config only the opposite direction Commented Jun 7, 2016 at 11:36
  • hey @Roman Koliada, my problem is, i can't get the value from my variable, and i need this value it to do some conditional config , any suggestion please?. Commented Jun 7, 2016 at 11:40
  • @Ali, I don't see a problem, it just works: plnkr.co/edit/xxTpvxB9GSwAqy25s5GN?p=preview Commented Jun 7, 2016 at 12:05

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

thank you @Itsik Mauyhas , the problem was in the order of calling, it works now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.