Adding Items to the Cart
You call the addToCart() function, shown in Listing 28.23, from the template when the user clicks the Add to Cart button. It iterates through the items in the customer.cart, and if it finds that the item is there, it increments the quantity; otherwise, it adds the item to the customer.cart array with a quantity of 1.
Once $scope.customer is updated, you call an $http POST to the /customers/update/cart route to update the cart. This way, the cart is persistent and will be there even if the user closes the browser or navigates away. On success, the view switches to cart.html. On failure, an alert window appears.
Listing 28.23 cart_app.js-addToCart: Adding controller functions to handle adding and removing products from ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access