Javascript Angularjs:为什么页面刷新会破坏 $rootScope 的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29849825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angularjs: Why page refresh destroy the values of $rootScope?
提问by gabrielAnzaldo
In my local route http://localhost:9000/#/deviceDetail/I have a controller that manage that view. Before going to that view I set some variables to the $rootScope(for example $rootScope.dashboards).
在我的本地路由http://localhost:9000/#/deviceDetail/我有一个管理该视图的控制器。在转到该视图之前,我将一些变量设置为$rootScope(例如$rootScope.dashboards)。
Once on that view I have acces to dashboards property, but when I refresh the page with F5key for example the property dashboards is lost.
在该视图上,我可以访问仪表板属性,但是当我使用F5密钥刷新页面时,例如属性仪表板丢失了。
I tried to save the $rootScopeon the localStorage variable but I got circular reference problems with the JSON.stringifymethod.
我试图将 保存$rootScope在 localStorage 变量上,但我遇到了该JSON.stringify方法的循环引用问题。
Any tip to manage that?
任何提示来管理?
回答by Marko Gre?ak
AngularJS is a JavaScript framework, everything is stored in memory heap and the heap is starts when you open a page and it's destroyed after you close it. In this context, browser refresh is like closing and re-opening the page.
AngularJS 是一个 JavaScript 框架,所有的东西都存储在内存堆中,当你打开一个页面时,这个堆就开始了,当你关闭它时它就会被销毁。在这种情况下,浏览器刷新就像关闭和重新打开页面。
To keep the value after refresh, you should store it in a cookie, for this you use for example $cookiesor sessionStorage/ localStorageas recommended by M K.
为了保持刷新后的值,你应该把它存放在一个cookie,这个使用例如$cookies或的sessionStorage/ localStorage的建议由M K.
回答by Thomas.Benz
I tried to store auth token using cookies following the article at Getting started with AngularJS and ASP.NET MVC - The long awaited Part Three. But the cookies is destroyed whenever I hit F5 or refresh the Chrome browser.
我尝试按照AngularJS 和 ASP.NET MVC 入门 - 期待已久的第三部分中的文章使用 cookie 存储身份验证令牌。但是每当我按 F5 或刷新 Chrome 浏览器时,cookie 就会被销毁。
That article says ngCookies helps to deal with page refresh to maintain token for page refresh. And I had thought it did and I did not know ngCookies killed me. It was destroyed if page is refresh! after hours to research online I see this article helps me.
那篇文章说 ngCookies 有助于处理页面刷新以维护页面刷新的令牌。我曾认为它确实如此,但我不知道 ngCookies 杀死了我。如果页面刷新,它被破坏了!经过几个小时的在线研究,我看到这篇文章对我有帮助。
According to M K, I used localStorage (or sessionStorage) helped me to get rid of the headache of cookies. Using cookies to store authentication token or something else is a bad idea. I ran into that problem and I got lost (did not know the bug coming from "using cookies") as the above article mentioned/confirmed. So, it was a bug in that article.
根据 MK 的说法,我使用 localStorage(或 sessionStorage)帮助我摆脱了 cookie 的头痛。使用 cookie 来存储身份验证令牌或其他东西是一个坏主意。正如上面提到/确认的那样,我遇到了这个问题并且迷路了(不知道来自“使用cookies”的错误)。所以,这是那篇文章中的一个错误。
Thank you million times, M K.
谢谢你百万次,M K。
回答by CYBERSIX
Use localStorage and $stateChangerStart check if you using ui.route something like this
使用 localStorage 和 $stateChangerStart 检查您是否使用 ui.route 这样的东西
$rootScope.$on('$stateChangeStart', function(event, toState) {
// Grab the user from local storage and parse it to an object
var user = JSON.parse(localStorage.getItem('user'));
if(user) {
$rootScope.currentUser = user;
}
});

