首页 > 解决方案 > 如何在 *ngFor 中的数组元素之间放置逗号?

问题描述

我想知道如何在数组元素之间放置逗号,而行尾没有逗号。有类似的问题,但我的有点不同,因为我使用 *ngFor 指令来显示文本:

<span *ngFor="let style of styles">
    {{style}} //If I put a comma after the text, there would be a comma at the end of the line after the  
              //last element was displayed
<span>

解决我的问题的方法是什么?

标签: javascripthtmlangulartypescriptangular-directive

解决方案


您可以使用以下last*ngFor

<span *ngFor="let style of styles; let last = last">
  {{style}}<ng-container *ngIf="!last">,</ng-container>
<span>

或者您可以使用以下first*ngFor

<span *ngFor="let style of styles; let first = first">
  <ng-container *ngIf="!first">,</ng-container> {{style}}
<span>

推荐阅读