Mikhail
Mikhail

Reputation: 769

Get non filtered index for ng-repeat element

Imagine a simple ng-repeat with custom filter function, that will filter all the values that has an index different from 2 and 4 for example. Like this:

<div ng-repeat="elem in arr | filter:myFilter() track by $index">{{$index}}</div>

The output would be <div>0</div><div>1</div>. But how can I display the real array indexes, like <div>2</div><div>4</div>?

Upvotes: 0

Views: 173

Answers (1)

TheSameSon
TheSameSon

Reputation: 369

You can't. $index always has value of filtered element. The only solution is to loop through unfiltered array and add your custom index property:

myService.getData().then(function (results) {
    angular.forEach(results, function (result, i) {
        result.index = i;
    });

    $scope.array = results;
});

Upvotes: 1

Related Questions