0

在 cookiestore 中存储数组时遇到问题。尝试将数组添加到 cookiestore 中,以便以后可以访问它。JS

angular.module('myApp', ['ngCookies']);
function CartForm($scope, $cookieStore) {
$scope.invoice.items = $cookieStore.get('items');
$scope.addItem = function() {
$scope.invoice.items.push({
    qty: 1,
    description: '',
    cost: 0
 });
$scope.invoice.items = $cookieStore.put('items');
},

 $scope.removeItem = function(index) {
 $scope.invoice.items.splice(index, 1);
 $scope.invoice.items = $cookieStore.put('items');
},

$scope.total = function() {
 var total = 0;
 angular.forEach($scope.invoice.items, function(item) {
     total += item.qty * item.cost;
 })

 return total;
 }
   }
4

1 回答 1

0

在cookie中存储数据:put(key, value);

angular.module('myApp', ['ngCookies']);

function CartForm($scope, $cookieStore) {

$scope.invoice.items = $cookieStore.get('items') || [];

$scope.addItem = function() {
    $scope.invoice.items.push({
        qty: 1,
        description: '',
        cost: 0
    });
    $cookieStore.put('items', $scope.invoice.items);
};

$scope.removeItem = function(index) {
    $scope.invoice.items.splice(index, 1);
    $cookieStore.put('items', $scope.invoice.items);
};

$scope.total = function() {
    var total = 0;
    angular.forEach($scope.invoice.items, function(item) {
        total += item.qty * item.cost;
    });
    return total;
};
}
于 2014-04-02T12:28:14.320 回答