【发布时间】:2020-11-22 22:14:27
【问题描述】:
如果“* ngFor”循环中的值为“true”(取自组件 ts 文件中的 response.body),我如何在 HTML 选项中将属性设置为“Selected”
下拉代码:
<select [(ngModel)]="customer.id">
<option *ngFor="let location of locations" [ngValue]="location.id"
[selected]="location.isDefaultLocation === true? 'selected': ''">
{{location.name}}
</option>
</select>
也试过了:
<option *ngFor="let location of locations" [ngValue] ="location.id"
[selected] ="location.isDefaultLocation === true">
{{location.name}}
</option>
也尝试过 [compareWith]:
<select [compareWith]="locationFn" [(ngModel)]="customer.id">
<option *ngFor="let location of locations" [ngValue]="location.id">
{{location.name}}
</option>
</select>
在组件 ts 文件中:
locationFn(item1: any, item2: any): boolean {
return item1.isDefaultLocation === true;
}
不起作用但总是返回第一个值!!
【问题讨论】:
标签: html typescript angular9 option angular10