2

I'm trying to get the absolute $index of the ng-repeat when using the filter. For example I have an array like this:

$scope.notes = [
    {
        name: 'note 1',
        value: '1'
    },
    {
        name: 'note 2',
        value: '2'
    },
    {
        name: 'note 3',
        value: '3'
    },
    {
        name: 'note 4',
        value: '4'
    }
];

Without applying the filter, the $index refers to the index of each element in the array and I use the $index to the array.

note in notes | filter:filterTerm track by $index

When I use the expression above the $index gets updated for the new sub-array. Is it possible to get the absolute $index?

4
  • could you assign an 'index' value to your raw data? Commented Aug 5, 2014 at 23:23
  • When you look at the $index of each note, what is shown if not the absolute index? Commented Aug 5, 2014 at 23:26
  • @haxxxton Unfortunately not :( Commented Aug 5, 2014 at 23:56
  • @user1518802 the new index in the sub-array Commented Aug 5, 2014 at 23:57

1 Answer 1

2

There is no 'absolute' $index here. The filter is returning a new array based on the array you passed in and the filter conditions.

Looking how you create your own custom filter makes this a bit easier to understand:

app.filter('myfilter',function()
{
  return function( items, filterArg) {
    var filtered = [];
    angular.forEach(items, function(item) {
      if(item.checkSomething(filterArg) {
        filtered.push(item);
      }
    });
    return filtered;
  };
}
Sign up to request clarification or add additional context in comments.

2 Comments

so there are no other way arounds?
On initalisation of your controller, you could iterate through your array and set a custom property with the original index

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.