值更改时计算ng重复的总和 [英] Calculate Sum in ng-repeat when Value Change
本文介绍了值更改时计算ng重复的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当值更改时,我要计算总和
When Value Changes I want to Calculate the Sum
<tr ng-repeat="act in ctrl.otsact.tests" ng-if="ctrl.editToggle">
<td>
<md-input-container>
<input type="text" ng-model="act.test_date" class="dateField" aria-label="Test Date">
</md-input-container>
</td>
<td ng-repeat="sub in act.subjects" >
<md-input-container>
<input type="number" ng-model="sub.score" aria-label="Score">
</md-input-container>
</td>
<td class="composite">
100
</td>
<td><span ng-click="ctrl.removeOTSACT(act.id)"> x </span></td>
</tr>
查看
Date of Test English Math Reading Science Writing Composite
2017-05-29 13 13 13 13 13 65
2017-05-29 2 2 2 2 2 10
要在加载时计算综合
推荐答案
您的视图:
<td class="composite">{{ getSum(act) }}</td>
控制器:
$scope.getSum = function(act){
var sum = 0;
for(var i = 0; i < act.subjects.length; ++i){
var subject = act.subjects[i];
sum += subject.score;
}
return sum;
}
当您使用ngModel指令或使用插值时,数据是双向绑定的,并且每次更改模型时都会重新计算函数值.
As you are using ngModel directive or using interpolation, your data is two-way-bound and function value will be recalculated every time you change your model.
这篇关于值更改时计算ng重复的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文