AngulaJS ng-repeat输入和求和

时间:2017-12-16 10:30:19

标签: javascript angularjs

我正在尝试使用Angular js创建功能。问题是我得到每个输入使用ng-repeat的重复数字,当我想要计算一个输入并从一些我早期得到的数字中减去

<tr ng-repeat="myNumbers in allMyNumbers track by $index">
  <td><input type="number" ng-change="vm.allSum(myNumbers.inputSum)" ng-model="myNumbers.inputSum" ng-disabled="disable"></td>
            </tr>

vm.allSum = function (int) {
        vm.someNumb = 345
        vm.someNumb = parseFloat(vm.someNumb - int);
    };

1 个答案:

答案 0 :(得分:0)

 angular.module('myApp', [])
    .controller('myCtrl', ['$scope', function($scope) {
      $scope.allMyNumbers = [{'inputSum':300},{'inputSum':200}]
      $scope.some_number  = 345
      $scope.enter_item ={}
      $scope.myFunc = function(obj,value) {
         	obj.inputSum_new      = (value - $scope.some_number)
      };
    }]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
<div ng-controller="myCtrl">
  <div ng-repeat="myNumbers in  allMyNumbers track by $index">
  orginal_value - {{myNumbers.inputSum}} ---------Enter_value - {{enter_item['val'+$index]||0}} -----Subtraction_val - {{some_number}}-----After subtraction result - {{'('+myNumbers.inputSum_new+')'}}<input ng-blur="myFunc(myNumbers,enter_item['val'+$index])"   ng-model="enter_item['val'+$index]" />

</div>