Add to Cart Functionality with AngularJS: Implementation Guide and Tutorial
☰Fullscreen
Table of Content:
Assignment: Perform Add to Cart with AngularJS
1. Perform Add to Cart with AngularJS
Add more features to the shopping application Pick2get:
• Add a field to capture the quantity to be ordered.
• Create an Add to cart button with a click function AddToCart (product), and return the success string with the alert Product added to Cart successfully.
• Enable Add to cart(change the image color over the button to orange) when the quantity field is > 0. Disable (change the image color over the button to grey) when the field is 0 or null.
Solution
{{pdt.discount}}Discount{{pdt.name}}Brand {{pdt.brand}}Price {{pdt.price}}
var app = angular.module('myApp', []); app.controller('myCtrl', function($scope) { var imgPath = "img/"; $scope.quantity=0; $scope.products = [ { name : "Happy Cycle", discount:"20%", price: "2500", brand : "Wheels", addedToCart:false, image : imgPath + "cycle.jpg", quantity:0 }, { name : "Kids Shoes", discount:"10%", price: "1460", brand : "Feel Good", addedToCart:false, image : imgPath + "shoes.jpg", quantity:0 }, { name : "Polo Baby Care Dress", discount:"20%", price: "2500", brand : "Super Hero", addedToCart:false, image : imgPath + "shirt.jpg", quantity:0 }, ] //Add your code here for addToCart function which returns success message $scope.addToCart = function(product) { if (product.quantity > 0) { product.addedToCart = true; alert(product.name + " added to cart successfully!"); } }; });
If you will run above code, you will get below output.