为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 *ngFor 中的数组元素之间放置逗号?

如何在 *ngFor 中的数组元素之间放置逗号?

红糖糍粑 2022-09-23 20:43:05
我想知道如何在数组的元素之间放置逗号,而行的末尾没有逗号。有类似的问题,但我有点不同,因为我使用*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>解决我的问题的方法是什么?
查看完整描述

2 回答

?
小唯快跑啊

TA贡献1863条经验 获得超2个赞

您可以使用以下值: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>


查看完整回答
反对 回复 2022-09-23
?
呼如林

TA贡献1798条经验 获得超3个赞

带有连接函数的 JS 方法(在本例中为 TS)。这样,当您在模板中时,字符串在单词之间已经有逗号。例如:


const str = ['one','two','three'];

const newStr = str.join(',');

console.log(newStr);//will output: one,two,three

角度方法是在模板中使用连接函数。在我看来,这是最易读的选择


<span>{{styles.join(',')}}</span>


查看完整回答
反对 回复 2022-09-23
  • 2 回答
  • 0 关注
  • 69 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信