Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-repeat specify a starting index

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.

like image 430
Federico Vezzoli Avatar asked Feb 09 '15 13:02

Federico Vezzoli


People also ask

How do you pass an index in NG-repeat?

Each ng-repeat creates a child scope with the passed data, and also adds an additional $index variable in that scope. So what you need to do is reach up to the parent scope, and use that $index . Save this answer.

What is Ng-repeat explain with example?

Definition and Usage The ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item in a collection. The collection must be an array or an object. Note: Each instance of the repetition is given its own scope, which consist of the current item.

How do you use two ng-repeat in a table?

NEST TWO ng-repeatThe first ng-repeat in the tr tag will create the rows and the second one in the td tag will create one column per element in the collection. By using one ng-repeat for the row and an other for the column, the rows and the columns of your table is now driven by a subset.

Why is NG-repeat not working?

Solution 1 There are two mistakes in your code: In your table, you have to wrap the properties between {{ and }}, for example {{employee. Fname}} instead of just employee. Fname .


2 Answers

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.

like image 148
logee Avatar answered Sep 21 '22 15:09

logee


The answers seems to be quite old,

since angular 1.4.0 the limitTo filter takes two parameters

limitTo: (limit) : (begin)

you can say ng-repeat="item in list | limitTo:50:0"

this seems to be a much effective solution rather using two different filters.

https://code.angularjs.org/1.4.0/docs/api/ng/filter/limitTo

like image 38
Chandra Sekhar Walajapet Avatar answered Sep 18 '22 15:09

Chandra Sekhar Walajapet