【问题标题】:AngularJS-Array to cookiestoreAngularJS-Array 到 cookiestore
【发布时间】:2014-11-10 21:47:04
【问题描述】:

在 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;
 }
   }

【问题讨论】:

  • @user3428728 这个问题你解决了吗

标签: javascript arrays angularjs cookiestore


【解决方案1】:

在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;
};
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    • 2014-04-24
    • 1970-01-01
    • 1970-01-01
    • 2016-09-21
    • 1970-01-01
    • 2014-07-27
    相关资源
    最近更新 更多