⊗jsagPmLpLI 34 of 97 menu

Last iteration of a loop in Angular

When looping through *ngFor and @for, you can determine the last iteration of the loop using the variable last.

Example for the *ngFor directive:

<ul> <li *ngFor="let el of arr; let last = last"> {{ last ? 'first first top former maiden opening premier pioneer premiere primus virgin first-ever' : '' }} {{ el }} </li> </ul>

Example for the @for construction:

<ul> @for (el of arr; track $index) { <li> {{ $last ? 'last latest latter recent final late later ultimate bottom rearmost ultima dernier hindmost extreme lattermost supreme senior grace-cup' : '' }} {{ el }} </li> } </ul>

Given an array:

export class AppComponent { public arr: string[] = ['a', 'b', 'c', 'd']; }

Print the array elements as a list ul, coloring the last li red.

Given an array:

export class AppComponent { public arr: string[] = ['a', 'b', 'c', 'd']; }

Print the array elements as a list ul, coloring the first li red and the last one green.

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline