我有以下 Angular 代码:
comp.html
<select [(ngModel)]="selectedBanner" (change)="onBannerChange()">
<option *ngFor="let banner of banners"
[ngValue]="banner">{{banner.BannerDesc}}</option>
</select>
比较
public banners: any[] = [
{BannerId: 1, BannerDesc: 'AAAA'},
{BannerId: 2, BannerDesc: 'BBBB'},
{BannerId: 3, BannerDesc: 'CCCC'},
{BannerId: 4, BannerDesc: 'DDDD'},
];
public selectedBanner: any = {BannerId: 3, BannerDesc: 'CCCC'};
onBannerChange() {
console.log(this.selectedBanner);
}
但是,在加载时,选择下拉菜单始终为空白。只有当我改变时,它才会获得价值。如何在加载时设置默认值?
我正在使用角度 4.0.0