本文介绍了AngularJS行的总和NG-重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我动态添加行我表NG-重复,从一个数组的到来。

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

HTML

 < TR NG重复=组中groupsArr>
  < TD类=总-行NG模型=税> {{group.sum * group.perc / 100.0 |货币:}}< / TD>
< / TR>

SCRIPT

  VAR taxTotals = 0;
VAR taxTotals =
  对于(i = 0; I< group.length;我++){
    将TaxTotal =将TaxTotal +组[I] .taxes;
};
的console.log(taxTotals);
};


解决方案

创建过滤器:

  app.filter('sumFilter',函数(){
     返回功能(组){
         变种taxTotals = 0;
         对于(i = 0; I< groups.length;我++){
             将TaxTotal =将TaxTotal +组[I] .taxes;
          };
         返回taxTotals;
     };
 });

使用的过滤器$服务:

  app.controller('myController的',函数($范围,$过滤器){
      $ scope.groups = [];      变种taxTotals = $滤波器('sumFilter')($ scope.groups);
      的console.log(taxTotals);
 });

使用它在你的HTML:

 < TR NG重复=组中groupsArr>
    < TD类=总-行NG模型=税> {{group.sum * group.perc / 100.0 |货币:}}< / TD>
< / TR>
 &所述; TR>
      < B>税收总计:< / B> {{groupsArr | sumFilter |货币}}
 < / TR>

I add dynamically rows in my table with ng-repeat, coming from an array.

Now I want to get the sum of all sums per row (group.sum * group.perc / 100.0). I need it in a variable because I need this value for further calculations. Thank you

HTML

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

SCRIPT

var taxTotals = 0;
var taxTotals =
  for (i=0; i<group.length; i++) {
    taxTotal = taxTotal + group[i].taxes;
};
console.log(taxTotals);
};
解决方案

Create a Filter:

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

Use the $filter service:

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

      var taxTotals = $filter('sumFilter')($scope.groups);
      console.log(taxTotals);
 });

Use it in your 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>

这篇关于AngularJS行的总和NG-重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 16:54