Search code examples
javascripthtmlangulartypescriptangular-http

How to start loop from index 1 instead of index 0 in *ngFor loop in angular?


I am currently getting some data from a dummy api, data from response starts from 1 and the index is currently starting from 0.

How I can start the index loop from 1 instead of 0?

Following the html for *ngFor below:

component.html

       <div class="all-users" >
            <div class="nested-items"  *ngFor="let data of flattenedResponse[0]; let i=index;  " (click)="switchUsers(i)">
                <h4>{{i}}</h4>&nbsp;
                <img src="{{data.image}}" alt="profile">
            </div>
        </div>

Solution

  • Why not just do the following:

           <div class="all-users" >
            <div class="nested-items"  *ngFor="let data of flattenedResponse[0]; let i=index;  " (click)="switchUsers(i)">
                <h4>{{i + 1}}</h4>&nbsp;
                <img src="{{data.image}}" alt="profile">
            </div>
        </div>
    

    I don't know of a way to change where the index starts, but this way works fine for most cases