9

Is it possible to keep variables on scope after refreshing my AngularJS page? How do I get this configured?

Thanks!

3
  • Whenever you make a change worth persisting after a refresh, you should also make a async call to the database to persist those changes.
    – pixelbits
    Jul 16, 2014 at 19:40
  • 3
    use localStorage, there is a lot of angular plugins you can use Jul 16, 2014 at 19:41
  • It might help embed.plnkr.co/doV9j7
    – Anil Singh
    Aug 11, 2016 at 11:26

2 Answers 2

3

There are several options to do this,

  1. For smaller data you could use the $cookieStore, for data that is under 4k
  2. Another option, especially with large data sets, would be to use Local Storage and then retrieve the data on page load/reload.
  3. if it is only a very small amount of data, or data that is used through out multiple page you could use $rootscope, but this is not the best option as it just like polluting the global name space.
  4. The last option, depending on how the data is retrieved, a service could be implemented, that is basiclly a singlton that can be passed to various angular scope.

All of this is dependent on the data in which you want to store. If you provide more info on what you are trying to achieve then the options could be narrowed.

2
  • 1
    I believe OP means browser refresh which counts out storing anything within javascript #3 & #4
    – charlietfl
    Jul 16, 2014 at 20:08
  • @charlietfl this may be true, however without more details about what the op wanted (ie browser refresh / page reload), I provided those options. Jul 16, 2014 at 21:17
1

I realize this is an old Question / Answer but after running into page refresh $scope and a multi-step form issues, I wanted to post what I found to assist. @Jared Reeves is correct above, but I think gsklee ngStroage is a fantastic tool for the question's requirements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.