0

I have some variables in angular controller. I have there if user is logged in and other details . When i am refreshing my page using f5 all my variables are reloading and i am getting in log in page, but before refresh i was already logged in and my variable isUserLoggedIn was true but after refresh it is false , false is this variables default value, I am declaring it in Controller .How can I save all my variables ( current point of angular controller ) and continue with my saved point after refresh ?

5
  • you can use localstorage Commented Aug 24, 2017 at 7:34
  • how can i do it using code can you tell me ? Can you explain it in more details ? Commented Aug 24, 2017 at 7:35
  • check my answer Commented Aug 24, 2017 at 7:37
  • you can use cookies, localstorage or sessionstorage of browser to store data Commented Aug 24, 2017 at 8:06
  • if i use localstorage like @Sachin Gupta said too, how can i catch refresh event to restore my variables from this localstorage ? Commented Aug 24, 2017 at 8:09

1 Answer 1

0

You can use the browsers local storage(HTML5) and store your data.

E.g: to write:

$window.localStorage['sample-key'] = 'sample-data;

to read:

var data = $window.localStorage['sample-key']

There are many libraries available for this, you can check this one: https://github.com/grevory/angular-local-storage

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

4 Comments

I must save all my variables like this ya ? For example $window.localStorage['userLogedIn']=true ; $window.localStorage['variable1'] = false; and so on , but where can i write this code ? how can i catch refresh event ?
you don't need to catch refresh event, as it is stored in browser itself.
yes but where can i locale this code ? and how can i know if my page has already refreshed and i must take data from localStorage, if i don't need to catch refresh event ?
I think you should first take a look at local storage, how it works. then I think it would be good for you to use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.