如何获取ng-init和ng-repeat中的总和-angularjs [英] How do I get the total sum in ng-init and ng-repeat - angularjs
问题描述
ng-init和ng-repeat angularjs有问题
我正在尝试循环使用字段速率以获取总值
例如这是我的桌子
nane +++++++ id +++++++ 费率
乔 +++++++++ 1 +++++++ 3
乔 +++++++++ 2 +++++++ 3
乔 +++++++++ 3 +++++++ 3
乔 +++++++++ 4 +++++++ 3
这是我的代码.
<table>
<tr>
<td>name</td>
<td>rate</td>
</tr>
<tr ng-repeat="item in videos">
<td>{{item.name}}</td>
<td ng-init="videos.total.rate = videos.total.rate + item.rate">{{item.rate}}</td>
</tr>
<tr>
<td>Total</td>
<td>{{videos.total.rate}}</td>
</tr>
加在一起时得到的结果是3333,而不是12
这是有问题的行
<td ng-init="videos.total.rate = videos.total.rate + item.rate">{{item.rate}}</td>
如果我将其更改为数字,则可以正常工作.
<td ng-init="videos.total.rate = videos.total.rate + 3">{{item.rate}}</td>
您的帮助会很棒.谢谢
在控制器中尝试类似的操作.
JS
$scope.RateTotal= 0;
for (var i = 0; i < data.length; i++) {
$scope.RateTotal= $scope.RateTotal + data[i].rate;
}
HTML
<p>{{RateTotal}}</p>
上面的选项更好,但是如果您想使用 ng-init 使用类似这样的内容.
<table ng-init="RateTotal = 0">
<thead>
<th>Rate</th>
</thead>
<tbody>
<tr ng-repeat="item in videos">
<td ng-init="$parent.RateTotal= $parent.RateTotal + item.rate">{{item.rate}}</td>
</tr>
<tr>
<thead>
<tr>
<th>Total</th>
<th>{{RateTotal}}</th>
</tr>
</thead>
</tr>
</tbody>
</table>
P.S.
此指令可被滥用以向其中添加不必要的逻辑 您的模板. ngInit只有少数适当的用法,例如 关于别名ngRepeat的特殊属性,如演示中所示 以下;以及通过服务器端脚本注入数据.除了这些 在少数情况下,您应该使用控制器而不是ngInit进行初始化 范围上的值. - ngInit
Having a problem with in ng-init and ng-repeat angularjs
i'm trying to loop the fields rates to get the total
for example this is my table
nane +++++++id+++++++rate
joe +++++++++1+++++++3
joe +++++++++2+++++++3
joe +++++++++3+++++++3
joe +++++++++4+++++++3
this my code.
<table>
<tr>
<td>name</td>
<td>rate</td>
</tr>
<tr ng-repeat="item in videos">
<td>{{item.name}}</td>
<td ng-init="videos.total.rate = videos.total.rate + item.rate">{{item.rate}}</td>
</tr>
<tr>
<td>Total</td>
<td>{{videos.total.rate}}</td>
</tr>
The Result that I get is 3333 instead of 12 when added all together
this is the line with problem
<td ng-init="videos.total.rate = videos.total.rate + item.rate">{{item.rate}}</td>
if i change it to a number it works fine.
<td ng-init="videos.total.rate = videos.total.rate + 3">{{item.rate}}</td>
your help would be great.thanks
Try something like this in controller.
JS
$scope.RateTotal= 0;
for (var i = 0; i < data.length; i++) {
$scope.RateTotal= $scope.RateTotal + data[i].rate;
}
HTML
<p>{{RateTotal}}</p>
Option above is better, but if you want use ng-init use something like this.
<table ng-init="RateTotal = 0">
<thead>
<th>Rate</th>
</thead>
<tbody>
<tr ng-repeat="item in videos">
<td ng-init="$parent.RateTotal= $parent.RateTotal + item.rate">{{item.rate}}</td>
</tr>
<tr>
<thead>
<tr>
<th>Total</th>
<th>{{RateTotal}}</th>
</tr>
</thead>
</tr>
</tbody>
</table>
P.S.
This directive can be abused to add unnecessary amounts of logic into your templates. There are only a few appropriate uses of ngInit, such as for aliasing special properties of ngRepeat, as seen in the demo below; and for injecting data via server side scripting. Besides these few cases, you should use controllers rather than ngInit to initialize values on a scope. - ngInit
这篇关于如何获取ng-init和ng-repeat中的总和-angularjs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!