Usar coma como separador de lista en Angular 2


Quiero crear una lista de elementos en mi plantilla, separados por comas, pero no quiero que el último elemento tenga una coma:

one, two, three

¿Cómo logro esto con la sintaxis de plantilla de Angular 2?

Author: Mark Rajcok, 2015-12-03

2 answers

Me gusta más la respuesta de Eric (ver su comentario para un Émbolo de muestra):

<span *ngFor="let item of items; let isLast=last">
   {{item}}{{isLast ? '' : ', '}}
</span>

Mi respuesta original fue usar el index opcional en el ngFor microsintax :

<span *ngFor="#item of items, #i=index">
   {{item}}{{i === items.length - 1 ? '' : ', '}}
</span>

Una alternativa es usar la sintaxis CSS ::before, como se describe aquí: https://stackoverflow.com/a/31805688/215945

 69
Author: Mark Rajcok,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:18:00

Creo que un enfoque más simple es

<span> {{items.join(", ")}} </span>
 26
Author: Nikhil Nambiar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-03-07 09:35:06