Introduction

In this blog, we will learn how to calculate shopping cart value in a simple way using AngularJS.

Step-1

Open the code editor of your choice (I used Visual Studio). Create a new project or file and give it a name.

Step-2

Add the script and style link to the head section of your created project or file.
  1. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  2. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
  3. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
  4. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
Step-3

Write a simple Angular script for calculation.
  1. var app = angular
  2. .module("myModule", [])
  3. .controller("myController", function ($scope) {
  4. $scope.invoice = {
  5. items: [{
  6. qty: 5,
  7. description: 'Product-1',
  8. cost: 120
  9. }]
  10. };
  11. $scope.addItem = function () {
  12. $scope.invoice.items.push({
  13. qty: 1,
  14. description: '',
  15. cost: 0
  16. });
  17. },
  18. $scope.removeItem = function (index) {
  19. $scope.invoice.items.splice(index, 1);
  20. },
  21. $scope.total = function () {
  22. var total = 0;
  23. angular.forEach($scope.invoice.items, function (item) {
  24. total += item.qty * item.cost;
  25. })
  26. return total;
  27. }
  28. });
Step-4

Design an HTML page.
  1. <body ng-controller="myController">
  2. <div class="container py-3">
  3. <h4 class="text-center text-uppercase">How to calculate Shopping Card in Angular js</h4>
  4. <table class="table table-bordered">
  5. <thead class="bg-danger text-white text-upparcase">
  6. <tr>
  7. <th>Product</th>
  8. <th>Quantity</th>
  9. <th>Price</th>
  10. <th>Total</th>
  11. <th></th>
  12. </tr>
  13. </thead>
  14. <tbody>
  15. <tr ng:repeat="item in invoice.items">
  16. <td>
  17. <input type="text" ng:model="item.description" class="form-control">
  18. </td>
  19. <td>
  20. <input type="number" min="1" ng:model="item.qty" ng:required class="form-control">
  21. </td>
  22. <td>
  23. <input type="number" min="1" ng:model="item.cost" ng:required class="form-control">
  24. </td>
  25. <td>{{item.qty * item.cost | currency:'₹'}}</td>
  26. <td>
  27. <a href ng:click="removeItem($index)" class="btn btn-sm btn-danger"><i class="fa fa-trash"></i></a>
  28. </td>
  29. </tr>
  30. <tr>
  31. <td><a href ng:click="addItem()" class="btn btn-sm btn-primary rounded-0">Add Product</a></td>
  32. <td></td>
  33. <td>Total:</td>
  34. <td>{{total() | currency:'₹'}}</td>
  35. </tr>
  36. </tbody>
  37. </table>
  38. </div>
  39. </body>
Complete code
  1. <!DOCTYPE html>
  2. <html ng-app="myModule">
  3. <head>
  4. <title>Shopping Card</title>
  5. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  6. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
  7. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
  8. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
  9. <script type="text/javascript">
  10. var app = angular
  11. .module("myModule", [])
  12. .controller("myController", function ($scope) {
  13. $scope.invoice = {
  14. items: [{
  15. qty: 5,
  16. description: 'Product-1',
  17. cost: 120
  18. }]
  19. };
  20. $scope.addItem = function () {
  21. $scope.invoice.items.push({
  22. qty: 1,
  23. description: '',
  24. cost: 0
  25. });
  26. },
  27. $scope.removeItem = function (index) {
  28. $scope.invoice.items.splice(index, 1);
  29. },
  30. $scope.total = function () {
  31. var total = 0;
  32. angular.forEach($scope.invoice.items, function (item) {
  33. total += item.qty * item.cost;
  34. })
  35. return total;
  36. }
  37. });
  38. </script>
  39. </head>
  40. <body ng-controller="myController">
  41. <div class="container py-3">
  42. <h4 class="text-center text-uppercase">How to calculate Shopping Card in Angular js</h4>
  43. <table class="table table-bordered">
  44. <thead class="bg-danger text-white text-upparcase">
  45. <tr>
  46. <th>Product</th>
  47. <th>Quantity</th>
  48. <th>Price</th>
  49. <th>Total</th>
  50. <th></th>
  51. </tr>
  52. </thead>
  53. <tbody>
  54. <tr ng:repeat="item in invoice.items">
  55. <td>
  56. <input type="text" ng:model="item.description" class="form-control">
  57. </td>
  58. <td>
  59. <input type="number" min="1" ng:model="item.qty" ng:required class="form-control">
  60. </td>
  61. <td>
  62. <input type="number" min="1" ng:model="item.cost" ng:required class="form-control">
  63. </td>
  64. <td>{{item.qty * item.cost | currency:'₹'}}</td>
  65. <td>
  66. <a href ng:click="removeItem($index)" class="btn btn-sm btn-danger"><i class="fa fa-trash"></i></a>
  67. </td>
  68. </tr>
  69. <tr>
  70. <td><a href ng:click="addItem()" class="btn btn-sm btn-primary rounded-0">Add Product</a></td>
  71. <td></td>
  72. <td>Total:</td>
  73. <td>{{total() | currency:'₹'}}</td>
  74. </tr>
  75. </tbody>
  76. </table>
  77. </div>
  78. </body>
  79. </html>
Step-5

Run your file or project on the browser.

Final output
output