【问题标题】:angularjs localstorage displaying dataangularjs localstorage显示数据
【发布时间】: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()。以防万一您使用的是

标签: html jquery angularjs


【解决方案1】:

你可以试试这个

var array = service.getItem('profiles');
for(var i=0;i<array.length;i++){
     console.log(array[i].name, array[i].email) 
} 

【讨论】:

    【解决方案2】:

    要放弃使用解析并处理任何这些,您可以尝试我最近创建的工厂,它可以让您存储和检索数组和对象。它还可以让你绑定到 $scope.variables:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-16
      • 2016-03-30
      • 1970-01-01
      • 2015-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多