这是我的代码
$scope.cart = [];
$scope.addToCart = function (cat) {
var found = false;
$scope.cart.forEach(function (item) {
if (item.id === cat.product_id) {
item.quantity++;
found = true;
}
});
if (!found) {
$scope.cart.push(angular.extend({quantity: 1}, cat));
}
};
//remove from cart function
$scope.removeToCart = function (cat) {
console.log(cat.product_id);
var found = false;
$scope.cart.forEach(function (item) {
if (item.id === cat.product_id) {
item.quantity--;
found = true;
}
});
if (!found) {
$scope.cart.push(angular.extend({quantity: 1}, cat));
}
};
console.log($scope.cart);
$scope.getCartPrice = function () {
var total = 0;
$scope.cart.forEach(function (cat) {
total += cat.finalprice * cat.quantity;
});
return total;
};
答案 0 :(得分:0)
您可能需要从removeToCart函数中删除此位:
if (!found) {
$scope.cart.push(angular.extend({quantity: 1}, cat));
}
如果" cat"在购物车中找不到,您将始终添加数量为1的商品。&& 34; addToCart"和"删除ToCart"除了item.quantity ++和item.quantity-- lines之外,对我来说似乎完全相同。