1

enter image description herei need to show total amount. first and second text box value sum should be display in total text box. have any way to do it.

    <input type="text" ng-model="add.amount1"/>
<input type="text" ng-model="add.amount2"/>
  Total  <input type="text" ng-model={{add.amount1+add.amount2}}/>

5
  • Why not handle changeevent and calculate total? Commented Mar 10, 2018 at 15:29
  • <input type="text" ng-model={{add.amount1+add.amount2}}/> this is not working Commented Mar 10, 2018 at 15:30
  • Possible duplicate of How to sum two fields in AngularJS and show the result in an label? Commented Mar 10, 2018 at 15:34
  • my way is not working. can u help me to show total amount Commented Mar 10, 2018 at 15:34
  • Look at the potential duplicate I proposed and use value instead of ng-model Commented Mar 10, 2018 at 15:36

2 Answers 2

1

You need to have a ng-change on text boxes and call a function to calculate the sum.

DEMO

var app = angular.module('testApp',[]);
app.controller('testCtrl',function($scope){
$scope.add = {};
$scope.add.amount1 = 0;
$scope.add.amount2 = 0;
$scope.calculateSum = function(){
  $scope.sum = parseInt($scope.add.amount1) + parseInt($scope.add.amount2);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="testApp" ng-controller="testCtrl">
 <input type="text" ng-change="calculateSum()" ng-model="add.amount1"/>
<input type="text" ng-change="calculateSum()" ng-model="add.amount2"/>
Total  <input type="text" ng-model="sum"/>
</body>

Sign up to request clarification or add additional context in comments.

8 Comments

Small issue. actually i have 5 textboxes. total amount displayed when entered last textbox value. but total should be display and change when changing textbox values
then have ng-change only for the last text box value
check question. i added screen shot.if i enter measurement 05, total displayed
use ng-disabled and enabe it on ng-chage on textbox 5
sorry i did not get u. what for ng-disabled.
|
0

I hope this can also be considered

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.name = "John Doe";
     $scope.add = {};
      $scope.add.amount1 = 1;
       $scope.add.amount2 = 2;
       $scope.sum = function(add) {
       
      
      return +add.amount1 + +add.amount2;
     }
});
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body ng-app='myApp'>
<div ng-controller='myCtrl'>

<input type="text" ng-model="add.amount1"/>
<input type="text" ng-model="add.amount2"/>
  Total  <input type="text"  value="{{sum(add)}}" />
</div>
 
</body>
</html>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.