我想总结不同ng-repeat中的三个值,这就是我所拥有的。
<tr ng-repeat="a in data1">
<td>a.num1</td>
</tr>
<tr ng-repeat="b in data2">
<td>b.num2</td>
</tr>
<tr ng-repeat="c in data3">
<td>c.num3</td>
</tr>
<tr>
<td>(Here I want to print the sum of the three values)</td>
</tr>
我的控制器中有这个。
$scope.data1 = [
{
"num1": 1,
}
]
$scope.data2 = [
{
"num2": 2,
}
]
$scope.data3 = [
{
"num3": 3,
}
]
绑定是好的,我打印来自每个ng-repeat的值,但我需要对值求和并将结果打印在最后
对此的一些帮助将是巨大的!
试试这个:
//在您的控制器端添加了此方法
$scope.getTotal = function(){
var total = 0;
angular.forEach($scope.data1, function(data1) {
total += data1.num1;
}
angular.forEach($scope.data2, function(data2) {
total += data2.num2;
}
angular.forEach($scope.data3, function(data3) {
total += data3.num3;
}
return total;
}
<td>{{ getTotal() }}</td>//Here is the total value
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句