1

我现在一直在尝试使用角度。我只是希望有人能帮助我为什么我不能像下面的代码那样获取本地存储数据。谢谢

[
    {
    "name": "firstname",
    "email": "email@yahoo.com"
    }
]

服务.js

 getItem: function (item) {

      var temp =  localStorage.getItem(item);

      if (!temp){
        return [];
      }

      return JSON.Parse(temp);

 }

控制器.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');
4

2 回答 2

0

你能试试这个吗

var array = service.getItem('profiles');
for(var i=0;i<array.length;i++){
     console.log(array[i].name, array[i].email) 
} 
于 2013-04-05T18:52:38.640 回答
0

To forego using parsing and deal with any of this you can try a factory I recently created, it let's you store and retrieve arrays and objects. Also it let's you bind to $scope.variables:

https://github.com/agrublev/Angular-localStorage

于 2013-05-20T23:58:59.320 回答