我想知道如何在数组的元素之间放置逗号,而行尾没有逗号。有类似的问题,但我的问题有点不同,因为我使用 *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>
解决我的问题的方法是什么?
您可以使用*ngFor
中的last
值:
<span *ngFor="let style of styles; let last = last">
{{style}}<ng-container *ngIf="!last">,</ng-container>
<span>
或者您可以使用*ngFor
中的first
值:
<span *ngFor="let style of styles; let first = first">
<ng-container *ngIf="!first">,</ng-container> {{style}}
<span>
带有连接函数的 JS 方法(在本例中为 TS(。 这样,当您在模板中时,字符串在单词之间已经有逗号。 例如:
const str = ['one','two','three'];
const newStr = str.join(',');
console.log(newStr);//will output: one,two,three
角度方法是使用模板内的连接函数。 在我看来,这是最具可读性的选择
<span>{{styles.join(',')}}</span>