Trying to parse localStorage (ngStorage) to ng-repeat

246 Views Asked by At

I've been playing around with AngularJS for a couple of days now and although some lightbulbs are starting to lightup i'm not sure why i cant use my localStorage variables just like my $scope variables in ng-repeat:

What is working:

JS

$scope.items = [
  {id: 1, title: 'a', desc: '1', quantity: 1, price: 14.50},
  {id: 2, title: 'b', desc: '2', quantity: 1, price: 25.95},
  {id: 3, title: 'c', desc: '3', quantity: 1, price: 22.50}
];

HTML

<div ng-repeat="item in items">
<span>{{item.title}}</span>
<span>{{item.price | currency:"&euro;"}}</span>
<span>{{item.price * item.quantity | currency:"&euro;"}}</span>
</div>

No problem so far. However when I store the same information in the $localStorage.items variables it is not parsed correctly any longer into the ng-repeat field. Other functions which will return a specific variable are working when i'm calling them as a function ({{example()}})

So what i'm trying to do:

JS

$scope.addToCart = function(index, title, desc, price) {
    var found = false;
    angular.forEach($localStorage.items, function(items) {
        if (items.id  === index) {
            items.quantity++;
            found = true;
        }
    });
    if (!found) {
        $localStorage.items.push(angular.extend({id: index, title: title, quantity: 1, price: price}, index));

    }
};

HTML

<div ng-repeat="item in $localStorage.items">
<span>{{item.title}}</span>
<span>{{item.price | currency:"&euro;"}}</span>
<span>{{item.price * item.quantity | currency:"&euro;"}}</span>
</div>

However this is not working. What am I doing wrong?

0

There are 0 best solutions below