Javascript can';不要在购物车上删除商品
我需要通过按清除按钮删除所有添加到购物车的项目。 我开始写一个函数,但它不起作用。 我认为错误是我没有把东西推到手推车上。 我如何做这两件事?无论如何,如果你看到任何错误,请提出来Javascript can';不要在购物车上删除商品,javascript,angularjs,Javascript,Angularjs,我需要通过按清除按钮删除所有添加到购物车的项目。 我开始写一个函数,但它不起作用。 我认为错误是我没有把东西推到手推车上。 我如何做这两件事?无论如何,如果你看到任何错误,请提出来 angular.module('TransactionApp', []) .controller('TransactionsCtrl', function($scope) { $scope.title = 'Add to cart'; $scope.itemsArray =
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;
};
});
Juse重置阵列中的项目
$scope.cart = [];
您不需要返回任何内容,您可以使用长度获得计数。只要重置数组中的项目
$scope.cart = [];
您不需要返回任何内容,您可以使用length获得计数。要使其为空,请使用以下命令:
$scope.cart = [];
或逐一减少:
$scope.cart.slice(start, end);//start is an index from where you want to delete and the end is number which represents how many to delete.
要在最后一个索引处在数组中插入内容,您确实需要推送:
$scope.cart.push(item);
要使其为空,请使用以下命令:
$scope.cart = [];
或逐一减少:
$scope.cart.slice(start, end);//start is an index from where you want to delete and the end is number which represents how many to delete.
要在最后一个索引处在数组中插入内容,您确实需要推送:
$scope.cart.push(item);
应该是
$scope.clearCart = function() {
$scope.cart = [];
};
应该是
$scope.clearCart = function() {
$scope.cart = [];
};