javascript - 无法从 Angular 上的购物车中删除商品
问题描述
我需要按清除按钮删除所有添加到购物车的物品。我开始编写一个函数,但它拒绝工作。我认为错误是我没有将物品推到购物车。我该怎么做这两件事?无论如何,如果您发现任何错误,请指出。
angular.module('TransactionApp', [])
.controller('TransactionsCtrl', function($scope) {
$scope.title = 'Add to cart';
$scope.itemsArray = [{
price: 50,
name: "Whey protein",
img: 'img/item-1.png',
quantity: 0
},
{
price: 60,
name: "Protein bar",
img: 'img/item-1.png',
quantity: 0
},
{
price: 35,
name: "BCAA",
img: 'img/item-1.png',
quantity: 0
},
{
price: 50,
name: "Whey protein",
img: 'img/item-1.png',
quantity: 0
},
{
price: 60,
name: "Protein bar",
img: 'img/item-1.png',
quantity: 0
},
{
price: 80,
name: "BCAA",
img: 'img/item-1.png',
quantity: 0
}
];
$scope.addTo = function(item) {
item.quantity += 1;
}
$scope.getCost = function(item) {
return item.quantity * item.price;
}
$scope.cart = [];
$scope.getTotal = function() {
return $scope.itemsArray.reduce((a, b) => a + b.price * b.quantity, 0);
}
$scope.clearCart = function() {
return $scope.cart.length = 0;
};
});
解决方案
应该
$scope.clearCart = function() {
$scope.cart = [];
};
推荐阅读
- linux - 检查启动时哪个配置文件设置了我的 PATH
- bash - 在 Bash 脚本中将多个文件作为参数传递并将其内容保存在两个数组中
- python - 如何预处理我的图像,以便 SVM 可以像处理 MNIST 数据集一样处理它
- node.js - 从 DynamoDB 检索值
- laravel - 卷曲错误 3:
格式错误 - Laravel 5.6 - r - 获取目录中所有 .Rmd 文件中加载的所有包的列表
- swift - 在 MapView 上的坐标处快速绘制文本
- python - pandas str.replace - 如果正则表达式无法避免 NaN 将字符串转换为数字,则保留当前值
- opengl - 在 OpenGL 3 / GLSL 130 中设置线宽
- python-3.x - 在 CSV 模块中使用“下一步”方法