首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AngularJS-到cookiestore的数组

AngularJS-到cookiestore的数组
EN

Stack Overflow用户
提问于 2014-03-17 20:16:47
回答 1查看 2.1K关注 0票数 0

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

代码语言:javascript
复制
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;
 }
   }
EN

回答 1

Stack Overflow用户

发布于 2014-04-02 20:28:14

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

代码语言:javascript
复制
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;
};
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22454049

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档