Control special characters at the end of a ngFor

0

I'm doing a website where I have to present the specialties of a chef, the data was collected from the backend and I present them with an ngFor in the following way:

 <div *ngFor="let i of item.specialty; let ultimo =last" >
  <p  style="display: inline-block;"> {{i.name}}, &nbsp; </p>
 </div>

As you can see add a comma to show the different specialties followed by a comma, the problem is that the last element also adds a comma, How can I control so that the comma does not appear in the last element that brings the ngFor?

    
asked by Gerardo Gutiérrez 05.09.2018 в 19:46
source

2 answers

0

You could put a comparison of if it's the last cycle then do not show it:

<div *ngFor="let i of item.specialty; let ultimo = item.specialty.length" >
  <p  style="display: inline-block;"> {{i.name}}{{ultimo == i ? '' : ','}} &nbsp; </p>
</div>
    
answered by 05.09.2018 в 19:56
0

The correct way to do it would be using the variable last the ngFor, I leave you below a link with the documentation of ngForOf, there you will find the use of last, first, etc;

<div *ngFor="let i of item.specialty; let ultimo = last" >
  <p  style="display: inline-block;"> {{i.name}}{{ultimo ? '' : ','}} &nbsp; </p>
 </div>

Documentation

    
answered by 17.09.2018 в 20:55