在ngStorage

时间:2015-09-25 10:20:50

标签: arrays angularjs ng-storage

我希望使用ngStorage存储数组以满足持久数据需求。这是我的代码:

.factory("items", function () {
    var items = {};
    items.data = [];
    return items; 
 })

.controller('ItemsController', function($scope,items) {
    $scope.items = items;
    $scope.addItem = function (id,item,size,price,quantity) {
        items.data.push({id,item,size,price,quantity});
   }
 })

我需要将items数组存储在ngStorage

1 个答案:

答案 0 :(得分:0)

.controller('ItemsController', function ($scope, items, $localStorage) {
  $scope.items = items;
  $scope.addItem = function (id, item, size, price, quantity) {
    items.data.push({id, item, size, price, quantity});
    localStorage.setItem("items", JSON.stringify(items));  
  }
});