我用的是
我想要实现的
问题
<select #selectedAlbum>
<option *ngFor="let album of albumList" [value]="folder.folder_title">{{album.album.title}}</option>
</select>
发布于 2017-09-04 15:49:05
您可以将select元素绑定到ngModel,然后在组件初始化时将ngModel设置为您想要的默认值,并通过ngModel提供的双向绑定为您选择该选项。
即:
<select #selectedAlbum [(ngModel)]="mySelectedItem">在你的部分:
mySelectedItem: string;
ngOnInit() {
this.mySelectedItem = 'album 2';
}发布于 2017-09-04 15:48:59
您可以添加一个选定的属性:
使用预定义项的示例
<select #selectedAlbum>
<option *ngFor="let album of albumList" [value]="folder.folder_title" [selected]="ablum.ablum.title === 'album 2'">{{album.album.title}}</option>
</select>使用索引的示例
<select #selectedAlbum>
<option *ngFor="let album of albumList; let i = index;" [value]="folder.folder_title" [selected]="i === 1">{{album.album.title}}</option>
</select>注意:
此方法不使用双向绑定。这是一个单向绑定,正如您目前已经实现的那样。如果您更喜欢使用双向绑定(这在一般情况下要优雅得多),我强烈建议深入研究表单模型。
https://stackoverflow.com/questions/46040448
复制相似问题