【发布时间】:2013-04-05 07:26:38
【问题描述】:
我现在一直在尝试使用 Angular。我只是希望有人能帮助我为什么我不能像下面的代码那样获取本地存储数据。谢谢
[
{
"name": "firstname",
"email": "email@yahoo.com"
}
]
service.js
getItem: function (item) {
var temp = localStorage.getItem(item);
if (!temp){
return [];
}
return JSON.Parse(temp);
}
controller.js
profile.push({
name: 'firstname',
email: 'rmail@yahoo.com'
});
localStorage.setItem('profiles', JSON.stringify(profile));
console.log(service.getItem('name') + ' : this should output the name');
console.log(service.getItem('email') + ' : this should output the email');
【问题讨论】:
-
在你的代码中调用 localstorage.setitem(key,value) 在哪里??
-
我编辑了代码。实际上它的工作正常,但现在它获取 json 值 raw = [{ "name": "firstname", "email":"email@yahoo.com"}] 我的问题是如何获取 name 和 email 的值.谢谢宁次
-
var array = service.getItem('profiles') for(var i=0;i<array.length;i++){ console.log(array[i].name, array[i].email) }你能试试这个吗? -
@rajkamal 它有效!你成功了!
-
仅供参考,AngularJS 有
angular.toJson()和angular.fromJson()。以防万一您使用的是