ng-repeat specify a starting index

Federico Vezzoli picture Federico Vezzoli · Feb 9, 2015 · Viewed 53k times · Source

How can I specify an index where an ng-repeat directive is going to start instead of zero?

I have a set of results and I just need to specify the starting one, which is different from zero.

Answer

kevin picture kevin · Jun 17, 2016

No need to code anything, angular has done this with the existing built-in limitTo filter. Just use a negative limit. From the docs for the limit argument:

The length of the returned array or string. If the limit number is positive, limit number of items from the beginning of the source array/string are copied. If the number is negative, limit number of items from the end of the source array/string are copied. The limit will be trimmed if it exceeds array.length. If limit is undefined, the input will be returned unchanged.

So you would use it like so in the template:

<ul>
   <li data-ng-repeat="i in list | limitTo: (offset - list.length)">{{i}}</li>
</ul>

where offset is your start index. See sample plunker.

There is an optional begin argument so you don't need use a negative limit but the begin was not available before angular v1.4 so I have stuck to a negative limit in my example.