Search code examples
javascriptangularjsangularjs-ng-repeat

AngularJS ng-repeat alias expression between multiple filters


According to Angular ngRepeat documentation, you can use the alias expression only at the end of ngRepeat:

Please note that `as [variable name] is not an operator but rather a part of ngRepeat micro-syntax so it can be used only at the end (and not as operator, inside an expression).

In my ng-repeat, I need to filter out items based on property, and then use limitTo filter for pagination purposes.

How can I get number of filtered items before the second filter?

This doesnt ofcourse work:

item in c.items | filter: c.contextFilter as filteredItems | limitTo: c.pagination.pageSize : c.pagination.currentPage*c.pagination.pageSize

Is there any other way I can get the filteredItems.length value ?


Solution

  • I had the same problem, I fixed it assigning the result of the first part of the filter expression into a new variable. In your example:

        item in filteredItems = (c.items | filter: c.contextFilter) | limitTo: c.pagination.pageSize : c.pagination.currentPage*c.pagination.pageSize```
    

    <div>{{filteredItems.length}}</div>

    Full example with controllerAs syntax:

       item in $ctrl.filteredItems = ($ctrl.items | filter: $ctrl.customFilter | 
       orderBy: $ctrl.order) | limitTo: $ctrl.pagination.pageSize : 
       $ctrl.pagination.currentPage*$ctrl.pagination.pageSize`