尝试将localStorage(ngStorage)解析为ng-repeat

时间:2016-02-15 23:05:52

标签: angularjs ng-storage

我已经和AngularJS玩了几天了,虽然有些灯泡开始点亮我不知道为什么我不能像ng-repeat中的$ scope变量一样使用我的localStorage变量:

工作原理:

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>

到目前为止没问题。但是,当我在$ localStorage.items变量中存储相同的信息时,它不再被正确解析为ng-repeat字段。当我将它们作为函数调用时,其他将返回特定变量的函数正在工作({{example()}})

所以我正在尝试做什么:

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>

然而这不起作用。我做错了什么?

0 个答案:

没有答案