简体   繁体   English

AngularJS:总结循环内子数组的值

[英]AngularJS: Sum up values of child array inside loop

I was trying to display values of AngularJS . 我试图显示AngularJS值。 Now I want to sum up all the values to its parent variable sum. 现在,我想将所有值加总为其父变量总和。

My AngularJs Array Looks Like: 我的AngularJs数组看起来像:

{  
   "isInternalInvoice":1,
   "name":"Invoices",
   "sum":50,
   "articles":[  
      {  
         "ID":"130.0000917-17.557000506.1",
         "betrag_netto":"20",
      },
      {  
         "ID":"130.0000917-17.557000581.1",
         "betrag_netto":"30"
      }
   ]
}

Code: 码:

<tr ng-repeat="invoice in currentInternalInvoices" >

 <input type="text" ng-model="invoice.betrag_netto"  />

</tr>

If I make changes in textbox it works: 如果我在文本框中进行更改,则可以使用:

<input type="text" ng-model="$parent.data.sum" ng-bind="$parent.data.sum"  />

But I want to sum up all the values while typing inside loop. 但是我想在循环内键入时总结所有值。

Just create a summing function 只需创建一个求和函数

$scope.getSum = function() {
  return $scope.data.articles.reduce((a, b) => a+Number(b.betrag_netto), 0);
}

and something to output it 和一些输出

<div>{{getSum()}}</div>

and it will automatically update it's value whenever you change the value in the input boxes. 并且只要您在输入框中更改值,它就会自动更新其值。

See this plunkr 看到这个笨蛋

Change the html to add a ng-change event listener which sums withe new value as the input field is updated. 更改html以添加一个ng-change事件侦听器,该侦听器在更新输入字段时会加上新值。

<tr ng-repeat="invoice in currentInternalInvoices" >

 <input type="text" ng-model="invoice.betrag_netto" ng-change="doSum()" />
</tr>
<input type="text" ng-model="data.sum"   />

Add this code in controller to handle the addition logic: 在控制器中添加以下代码以处理附加逻辑:

$scope.doSum = function() {
    $scope.data.sum = $scope.data.articles.reduce((a, b) => a+Number(b.betrag_netto), 0);
  }

You could create a custom directive 您可以创建一个自定义指令

 (function() { "use strict"; class MyController { constructor(dataService) { this.dataService = dataService; this.data = { sum: 0 } } $onInit() { this.data = this.dataService.get(); console.info('running'); } static get $inject() { return ['dataService']; } } const app = angular.module('app', []) .factory({ dataService }) .controller({ MyController }) .directive({ sum }); function sum() { return { restrict: 'A', bindToController: { sum: '=', sumSource: '=', sumBy: '@', }, scope: true, controller: class { $doCheck() { this.sum = this.sumSource.reduce((sum, { [this.sumBy]: value }) => Number(sum) + Number(value), 0); } } } } function dataService() { return { get() { return { "isInternalInvoice": 1, "name": "Invoices", "sum": 50, "articles": [{ "ID": "130.0000917-17.557000506.1", "betrag_netto": "20", }, { "ID": "130.0000917-17.557000581.1", "betrag_netto": "30" } ] }; } }; } }()); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script> <div ng-app="app" ng-controller="MyController as c"> <table sum="c.data.sum" sum-by="betrag_netto" sum-source="c.data.articles"> <tr ng-repeat="item in c.data.articles"> <td> <input type="text" ng-model="item.betrag_netto"> </td> </tr> </table> {{c.data | json}} <div></div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM