我正在开发应用程序使用角js。我在使用本地存储时遇到了问题。我的代码看起来:
var id = response.data[0].id;
var email = response.data[0].email;
localStorage.setItem('userId', id);
localStorage.setItem('email', email);我在本地存储中设置了两个值。
run(function($rootScope,$location) {
var userId = localStorage.getItem('userId');
$rootScope.$on('$locationChangeStart', function (event, next, current) {
alert(userId);
});
})我的问题是,当我刷新页面时,userId第一次显示为null,然后它会提醒我值。如何在不刷新页面的情况下获得值。
发布于 2015-10-29 12:16:05
我得到了解决方案.Problem是在$locationChangeStart‘方法之外获得本地存储。
以下是正确的方法:
run(function($rootScope,$location) {
$rootScope.$on('$locationChangeStart', function (event, next, current) {
var userId = localStorage.getItem('userId');
alert(userId);
});
})发布于 2015-10-29 12:13:51
我不知道这是否有用,但我找到了这个本地存储的例子。您可以打开两个页面,这两个页面都将根据另一个页面进行更新。本地存储示例,您将在右下角找到一个指向源代码的链接。
发布于 2015-10-29 12:06:24
$window.localStoage.setItem("key","Value");https://stackoverflow.com/questions/33413462
复制相似问题