Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular start ngFor index from 1

Tags:

angular

Is it possible to start ngFor index from 1 instead of 0?

let data of datas;let i=index+1 

didn't work.

like image 759
TheUnreal Avatar asked Aug 20 '16 17:08

TheUnreal


Video Answer


2 Answers

 *ngFor="let item of items | slice:1; let i = index; 

SlicePipe

like image 52
k2fx Avatar answered Sep 17 '22 12:09

k2fx


There are 2 possible answers to the question, depending on what was actually being asked.

If the intent is to skip the first element of the array, then the answers involving slice are in the right direction.

However, if the intent is to simply shift the index while still iterating over all of the array, then slice is NOT the correct approach, as it will skip the 0th element in the array, thereby outputting only n-1 items from an array of length n.

@Taylor gave a real-world example of when the index might need to be shifted for display purposes, such as when outputting a list where the first entry should read 1, not 0.

Here's another similar example:

<li *ngFor="let book of books; let i = index">     {{ i + 1 }}.  {{ book.title }} </li> 

which would produce output like:

  1. Sample Book Title

  2. Another book title

...

like image 38
Josh Black Avatar answered Sep 16 '22 12:09

Josh Black