关于如何正确计算视图中重复的记录的快速问题?
以下是这样的观点:
<h4> Drafts </h4>
<ul class="nav nav-pills scrollable-nav">
<li ng-repeat="solution in solutions | filter: {'username': username}: true | filter: {visible: false} ">
<a href="#" onclick="return false;">
<h5 ng-click="editSolution(solution.$id)">{{solution.name}}</h5>
</a>
</li>
</ul>
我想知道这个解决方案被重复了多少次?用户可以随时将solution.visible值更改为true,因此我需要动态显示该数字。
我可以在范围内使用一个变量来跟踪这个数字,但是我想知道是否还有其他更好的方法来实现它呢?
发布于 2016-01-16 10:36:04
您可以创建一个临时变量,该变量保存筛选结果的值。
<li ng-repeat="solution in filteredSolutions = (solutions | filter: {'username': username, visible: false}: true)">
然后执行{{filteredSolutions.length}}
以获得计数
您还可以使用替代方法,如@Claies建议的那样,对过滤后的结果进行混叠,这在角1.3+中得到了支持
<li ng-repeat="solution in solutions | filter: {'username': username, visible: false}: true as filteredSolutions">
发布于 2016-01-16 11:19:04
有时,在控制器中对数据进行过滤更方便。你可以这样做:
<div ng-controller="ListCtrl as ctrl">
<ul>
<li ng-repeat="person in ctrl.people" ng-click="ctrl.hide(person)">
{{person.name}}: {{person.phone}}
</li>
</ul>
<div>
Records count: {{ctrl.people.length}}
</div>
</div>
主计长:
app.controller('ListCtrl', ['$scope', '$filter', function ($scope, $filter) {
var self = this;
this.hide = function(item) {
item.visible = false;
self.refresh();
};
this.refresh = function() {
self.people = $filter('filter')(people, {visible: true});
};
this.refresh();
}]);
这样,您只需在控制器变量中获得过滤过的数据,就可以使用它来显示记录计数。
这是摆弄演示。
https://stackoverflow.com/questions/34830618
复制