Number of items in a list filtered AngularJS

前端 未结 4 1495
谎友^
谎友^ 2020-12-15 05:18

How do you get the length/number of items returned by a filter predictate in AngularJS?

相关标签:
4条回答
  • 2020-12-15 05:58

    As a matter of facts, this will work with display filter(orderBy, limitTo)

    item in list = ( item_array|filter:some_str) | orderBy: pref | limitTo: nItemToDisplay
    

    if you need to display 5 items at a time, you still can use list.length. Usefull for pagination or in datatable.

    0 讨论(0)
  • 2020-12-15 06:06

    I managed to find a great answer on the AngularJS Google Group, thanks to Pawel Kozlowski.

    ng-repeat="item in filtered = (items | filter:filterExpr)"
    

    Would create the filtered list on the fly, you can use filtered.length anywhere else in the current scope to show the count.

    0 讨论(0)
  • 2020-12-15 06:07

    i also had to display the length of a filtered array and this one worked for me:

    {{ (items | filter:filterExpr).length }}
    
    0 讨论(0)
  • 2020-12-15 06:08

    Solution

    Try this :

    HTML

    <span ng-bind="nb"></span>
    

    Javascript

    $scope.nb = $filter('filter')(items, filterExpr).length;
    

    Documentation

    In HTML Template Binding

    {{ filter_expression | filter : array : expression : comparator}}
    

    In JavaScript

    $filter('filter')(array, expression, comparator)
    
    0 讨论(0)
提交回复
热议问题