$rootscope value used in a controller is overridden when refreshing the page used with that controller

后端 未结 2 782
醉话见心
醉话见心 2021-01-13 22:36

In my angularjs page I am passing a global data to another controller using the $rootscope object of angularjs. In my controller i am successfully able to catch the value st

相关标签:
2条回答
  • 2021-01-13 23:10

    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
    0 讨论(0)
  • 2021-01-13 23:15

    Ok Utpal a few things to note here:

    • AngularJS will not persist between a full page reload, you have to use another mechanism to persist data between two different loads, like HTML5 localStorage or Cookies etc. The reason why refreshing the browser deletes your data is because the entire AngularJS app is being torn down and reloaded, so can't expect it to maintain state.

    • If you want to persist data between controllers, $rootScope isn't the best way. You should look up angularJS services and use them to communicate between controllers.

    Hope this helps!

    0 讨论(0)
提交回复
热议问题