Variables become undefined after page reloading

来源:互联网 发布:长沙网络公关 编辑:程序博客网 时间:2024/06/04 18:44

In current angularJS project, I stored some constants in a global array, while that array is set to undefined after I refresh the page.


The explanation I found online: 

http://stackoverflow.com/questions/21374643/rootscope-value-used-in-a-controller-is-overridden-when-refreshing-the-page-use

If you reload your page all information that you store in JavaScript variables are lost. $rootScope is nothing more then a variable in JavaScript. If you want to store something that persists a page reload there are at least the following possibilities:

  • use a Cookie (in angular you could use the $cookieStore)
  • use some of the new HTML5 features like Locale Storage, Web DB or the old DOM Storage (http://en.wikipedia.org/wiki/Web_storage)
  • store your data on a server
Then I selected a plugin angular-local-storage as the solution.
https://github.com/grevory/angular-local-storage

0 0