Angular.js. How to count ng-repeat iterations which satisfy the custom filter

落爺英雄遲暮 提交于 2019-11-26 22:40:20

ng-repeat expression allows filtered results to be assigned to a variable. This variable will be accessible from current scope so you can use it in same scope anyway you want:

<p>Number of filtered items: {{filteredItems.length}}</p>

<div 
  ng-show="filteredItems.length > 0" 
  ng-repeat="item in filteredItems = (items | notEmpty)"
>
 {{$index}}
</div>

The easiest way may be to run the filter in your controller. Something like this:

function MyCtrl($scope, notEmptyFilter)
{
    //$scope.items is put into the scope somehow
    $scope.filteredItems = notEmptyFilter($scope.items);
}

Then your HTML would look something like this:

<div ng-show="filteredItems.length > 0" ng-repeat="item in filteredItems">
</div>

As of Angular 1.3 you can use following syntax for ng-repeat:

variable in expression as alias_expression

That is:

<p>Number of filtered items: {{filteredItems.length}}</p>

<div ng-repeat="item in items | notEmpty as filteredItems">
 ...
</div>

I found this

How to display length of filtered ng-repeat data ,

that describes to get the count after filtering the list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!