简体   繁体   English

从html5本地存储中读取JSON

[英]Reading JSON from html5 local storage

I have created some JSON data and stored them in the local storage of html5: 我创建了一些JSON数据并将其存储在html5的本地存储中:

Key                    Value
storage1               {"pnumber": "0001", "branchid": "1"}
storage2               {"pnumber": "0002", "branchid": "2"}
storage3               {"pnumber": "0003", "branchid": "3"}

I would like to show those data in my html by using the ng-repeat function from angularjs. 我想通过使用angularjs的ng-repeat函数在HTML中显示这些数据。 But I'm still not sure how to do it. 但是我仍然不确定该怎么做。 Below is my failed method. 以下是我失败的方法。

Controller function from the js file snippet. js文件片段中的控制器功能。

.controller('TicketListCtrl', function($rootScope) {

   for(var i=1;i<=3;i++)
   {
       var ticlist = JSON.parse(localStorage.getItem('storage' + i));
       $rootScope.numbers = ticlist;
   }
});

My html file snippet: 我的html文件片段:

    <div class="list">
        <a class="item item-icon-left dark" href="#/app" ng-repeat="number in numbers">
         <p>This is {{number.pnumber}}</p>
        </a>
    </div>

But it could not show the pnumbers. 但是它不能显示pnumbers。 Do you guys know what's wrong? 你们知道怎么了吗?

numbers is an array so you need to push new values in it, instead of assigning it everytime. numbers是一个数组,因此您需要在其中推送新值,而不是每次都分配它。

 $rootScope.numbers = [];
 for(var i=1;i<=3;i++)
 {
   var ticlist = JSON.parse(localStorage.getItem('storage' + i));
   $rootScope.numbers.push(ticlist);
 }

看起来numbers应该是一个对象数组,但是是单个对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM