Is it possible to start ngFor index from 1 instead of 0?
let data of datas;let i=index+1
didn't work.
*ngFor="let item of items | slice:1; let i = index;
SlicePipe
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:
Sample Book Title
Another book title
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With