I am trying to figure out how to delete all items added to the cart by clicking on a clear button. I started writing a function for this, but it doesn't seem to be working. I think the issue might be that I forgot to push the items to the cart. How can I accomplish these tasks? If you notice any mistakes, please let me know.
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;
};
});