AngularJS Dynamic Product Add with Calculation

AngularJS Dynamic Product Add with Calculation

The post will shows how to add product dynamically with calculation using AngularJS. The code given below is created by one of our best student Abhijit Prasad.

Screen shot AngularJS Dynamic Product Add

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>Angular JS Dynamic Product Cart System</title>
<script src=""></script>
background-color: #ffea00;
/*border: 1px solid black; */
width: 100%;
#total-section tr,td{
background: blue;
color: white
text-align: center;
color: #f30;
text-align: center;
font-weight: bold;
color: #f40;
width: 100%;
#table tr,td{
border:1px solid #333;
#table tr,th{
border:1px solid #333;
background: black;
color: white;
#table .left{
text-align: right;
bottom-margin: 20px;
<h1>Angular JS Dynamic Product Cart System</h1>
<p>By Abhijit Prasad (Under guidance of Sanjay Sir (Codentricks))</p>

<div ng-app='myapp' ng-controller='myctrl'>
<input type="text" ng-model='prodname' placeholder="Product Name">
<input type="text" ng-model='qty' value="1" placeholder="Quantity">
<input type="text" ng-model='rate' value="0" placeholder="Rate">
<input type="text" ng-model='cgst' placeholder="CGST">%
<input type="text" ng-model='sgst' placeholder="SGST">%
<button style="width: 150px" ng-click="addItem()">Add</button><br><br>
<div style="width: 100%">
<table id="table">
<th>Total Cost</th>
<th>Line Total</th>
<tr ng-repeat='item in items'>
<td class="left">{{}}</td>
<td><button class="full-width" ng-click="removeItem('{{item.index}}')">Remove</button></td>
<table id='total-section'>
<td>Total Amount</td>
<input type="number" ng-model='discount'>
- {{discount}}
Grand Total
<td>{{totalCost() - discount}}</td>

var app = angular.module('myapp',[]);
app.controller('myctrl', function($scope){
$ = 0;
$scope.items = [];

$scope.price= "hello";
$scope.subtotal = function(){
return $scope.price = $scope.qty*( $scope.rate*(1 + $scope.cgst*0.01 + $scope.sgst*0.01)) ;

$scope.addItem = function (){
var item = {}; = $scope.prodname;
item.qty = $scope.qty;
item.rate = $scope.rate;
item.cgst = $scope.cgst ;
item.sgst = $scope.sgst;
item.totcost = item.qty * item.rate;
item.linetot = item.totcost * (1 + item.cgst*0.01 + item.sgst*0.01);
item.index = $scope.items.length;

$scope.removeItem = function (index){
$scope.totalCost = function(){
var sum=0;
for (var i = $scope.items.length - 1; i >= 0; i--) {
sum += $scope.items[i].linetot;
return sum;

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Do you have any Query / Suggestion