Coolguy
Coolguy

Reputation: 2285

Reading JSON from html5 local storage

I have created some JSON data and stored them in the local storage of 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. But I'm still not sure how to do it. Below is my failed method.

Controller function from the js file snippet.

.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:

    <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. Do you guys know what's wrong?

Upvotes: 1

Views: 53

Answers (2)

Grilse
Grilse

Reputation: 3917

Looks like numbers should be an array of objects, but is a single object instead.

Upvotes: 0

Zee
Zee

Reputation: 8488

numbers is an array so you need to push new values in it, instead of assigning it everytime.

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

Upvotes: 2

Related Questions