Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ngFor on a list without creating an intermediate variable

Is there a way to make an *ngFor without creating an intermediate variable : In the example below I declare item but I don't use it, is there a way to remove it?

<div *ngFor="let item of items; let i = index">
   <span>{{items[i]}}</span>
   <span>{{items[i+1]}}</span>
</div>
like image 560
Brahim LAMJAGUAR Avatar asked Apr 30 '26 09:04

Brahim LAMJAGUAR


1 Answers

You could use the ngFor template syntax, without the let-item part:

<ng-template ngFor [ngForOf]="items" let-i="index">
  <div>
    <span>{{items[i]}}</span>
    <span>{{items[i+1]}}</span>
  </div>
</ng-template>

See this stackblitz for a demo.

For more explanations about ngFor, *ngFor and ng-template, see this article by Todd Motto.

like image 75
ConnorsFan Avatar answered May 02 '26 00:05

ConnorsFan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!