Angular: *ngFor set iterator size

Issue

I’m trying to do a for loop in angular, i know how it works *ngFor="let item of items;let i = index"
But how can I say that I want my index to do +2 increment, so i would be 0,2,4,6…
How can I do that please ?

The equivalent in C

for(int i=0;i<=100;i+=2)
{
  my code;
}

items is a list of all my items, lets say that in my for loop I just displat my item, what i wanna do with that for loop is to print the first item, the third, the fifth …

I tried in my html page to use this syntax item[index].whatever but it doesn"t work :'(

Please help

Thanks

Solution

If you want to display index of 0,2,4,6…

   <div *ngFor="let item of items; let i = index">
            <span *ngIf="i % 2 === 0">Index: {{i}}</span>
   </div>

If you want to display index of 1,3,5,7…

   <div *ngFor="let item of items; let i = index">
            <span *ngIf="i % 2 !== 0">Index: {{i}}</span>
   </div>

Answered By – Myat Thiha

Answer Checked By – Mildred Charles (AngularFixing Admin)

Leave a Reply

Your email address will not be published.