这是一个代码片段
<span *ngFor="let rel of item.daysOfWeek">{{rel.name}} ,</span>
如何从item.daysOfWeek的所有值中获取一个值,然后不使用ngFor进行显示?
答案 0 :(得分:2)
您可以在daysOfWeek
数组上使用join
和map
方法。
尝试一下:
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
item = {
daysOfWeek: [
{ name: 'Monday' },
{ name: 'Tuesday' },
{ name: 'Wednesday' },
{ name: 'Thursday' },
{ name: 'Friday' },
{ name: 'Saturday' },
{ name: 'Sunday' },
]
}
daysOfTheWeek = this.item.daysOfWeek.map(rel => rel.name).join(' ,');
}
在您的模板中:
<span>{{ daysOfTheWeek }}</span>