0

I want so set a $rootScope variable stateSelection value default as false.

So when my applciation runs the value of the $rootScope.stateSelection will be false. I want to know how to set it at the time of initialization of app.

Right now i am setting it in my Auth Service in login function.

When User logs in & selects a state i call a method in a controller named clickSelection() & in this function i change the $rootScope.stateSelection = true;.

I get a problem when i refresh the page, the value of $rootScope.stateSelection becomes false.

Thanks

Anup
  • 9,396
  • 16
  • 74
  • 138

1 Answers1

1

$rootScope.stateSelection will always be false on refresh unless you're persisting the value somewhere.

If you are persisting the data somewhere, you can use the resolve property (read more about resolves here ) to return a function that will set the value before loading the page:

$stateProvider
  .state('home', {
    url: '/home',
    templateUrl : 'views/home.html',
    controller : 'HomeController',
    resolve : {
      authenticate : function auth(authService){
         // Function to get the stateSelection value
         return authService.getStateSelection()
      }
    }
  }
JoshSGman
  • 529
  • 2
  • 6
  • 16
  • Is there any other way around. if i can set some value globally & change it in a controller. – Anup May 11 '14 at 13:33
  • @Anup Even if you set something globally, if there isn't any persistence, the variables will reset on page refresh. An option, if you don't want to interact with a server, is to use localstorage for persistence, you can read more about that here : https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage#localStorage – JoshSGman May 11 '14 at 14:09