AngularJS行的总和ng-repeat

mm1*_*975 2 javascript angularjs

我使用ng-repeat动态地在表格中添加来自数组的行.

现在我想获得每行所有总和的总和(group.sum*group.perc/100.0).我需要在变量中,因为我需要这个值进行进一步的计算.谢谢

HTML

<tr ng-repeat="group in groupsArr">                                         
  <td class="total-rows" ng-model="taxes">{{group.sum * group.perc / 100.0 | currency :""}}</td>
</tr>
Run Code Online (Sandbox Code Playgroud)

脚本

var taxTotals = 0;
var taxTotals = 
  for (i=0; i<group.length; i++) {
    taxTotal = taxTotal + group[i].taxes;    
};
console.log(taxTotals);
};  
Run Code Online (Sandbox Code Playgroud)

pix*_*its 8

创建一个过滤器:

 app.filter('sumFilter', function() {
     return function(groups) {
         var taxTotals = 0;
         for (i=0; i<groups.length; i++) {
             taxTotal = taxTotal + groups[i].taxes;    
          };
         return taxTotals;
     };
 });
Run Code Online (Sandbox Code Playgroud)

使用$ filter服务:

 app.controller('myController', function($scope, $filter) {
      $scope.groups = [...];

      var taxTotals = $filter('sumFilter')($scope.groups);
      console.log(taxTotals);
 });
Run Code Online (Sandbox Code Playgroud)

在HTML中使用它:

<tr ng-repeat="group in groupsArr">                                         
    <td class="total-rows" ng-model="taxes">{{group.sum * group.perc / 100.0 | currency :""}}    </td>
</tr>
 <tr>
      <b> Tax Totals: </b> {{ groupsArr | sumFilter | currency }}
 </tr>
Run Code Online (Sandbox Code Playgroud)