Angular : ngFor items and append a char for all items except the last item

Issue

I am looping my data list and displaying in the view in spans :

 <span  *ngFor="let d of myData"> {{d.name}} ,</span>

As you can see , I am adding a comma ‘,‘ in the end of every item to obtain a coherent view

this results my in this appearance :

AAA,BBB,CCC,DDD,

My problem is with **the last comma which I want to automatically remove.

Suggestions ?

Solution

Use the last local variable, like so:

<span *ngFor="let d of myData; last as isLast"> {{d.name}} <span *ngIf="!isLast">,</span></span>

Readmore

Answered By – vicbyte

Answer Checked By – Clifford M. (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.