我可以ng-repeat
像这样计算列表中的属性总和:
<table ng-init="total=0">
<tr ng-repeat="item in items">
<td ng-init="$parent.total = $parent.total + item.Amount">
{{item.Amount}}
</td>
</tr>
</table>
但是,这不起作用,当items
被过滤时:ng-repeat="item in items | filter: search"
原因是,该<table>
元素只初始化一次,而<tr>
每次列表都被过滤。因此,应用过滤时总和不会设置为 0。
是否有一些在应用过滤时调用的事件/指令?我经常使用这个“计算总数”功能,并希望避免calculateTotals()
在我的控制器和指令中多次编写函数。