使用角度js获取文本框总和

时间:2018-03-10 15:27:31

标签: angularjs

enter image description here我需要显示总金额。第一个和第二个文本框值sum应显示在总文本框中。有任何办法。



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

2 个答案:

答案 0 :(得分:2)

您需要对文本框进行ng-change并调用函数来计算总和。

<强>样本

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>

答案 1 :(得分:0)

我希望这也可以考虑

&#13;
&#13;
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;
     }
});
&#13;
<!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>
&#13;
&#13;
&#13;