Angular, limitTo and track by $index

I am trying to limit an object coming in by filtering (because there will be an option to show all eventually), however I am running into issues when trying to limitTo and track by index. Here is the code :

 <div ng-repeat="item in filter.values track by $index | limitTo:filterLimit" class="cengage-builder-result-filter-value" value="item" update-filter="updateFilter">

In the controller:

  $scope.filterLimit = 5;

It’s saying I have dupes in the angular error so I’m thinking the track by $index isn’t working here. Can’t seem to find a proper way to do this, could use some help. Thanks!

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Filters, like limitTo, orderBy, etc… must come before track by, since they apply to the array source, rather than to the track by expression.

<div ng-repeat="item in filter.values | limitTo:filterLimit track by $index">

Solution 2

Try this
Use limitTo
before track by

 <div ng-repeat="item in filter.values | limitTo:filterLimit track by $index" class="cengage-builder-result-filter-value" value="item" update-filter="updateFilter">

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply