<div *ngFor="let lib of library">
<input type="text" [(ngModel)]="lib.item"></div>
<div>
<md-select [(ngModel)]="lib.title">
<md-option *ngFor="let book of books" [value]="book._id">{{book.bookname}}
<md-option>
</md-select>
</div>
</div>
我的控制器中有
books=[
{_id: 1, bookname:'first book'},
{_id: 2, bookname:'second book'},
{_id: 3, bookname:'third book'}
]
接口是
export interface Ixyz{
_id: string;
item: string;
title: ICat;
}
所以当我推类似的东西
var add:Ixyz={
_id: '',
item:'',
title: 2
}
我希望下拉列表中具有默认值。我知道如何使用ngModel
来寻找价值。但是ngModel
的用途有所不同。
当我使用title = 2推送新值时,它不会在下拉菜单中显示,因为title:ICat引用了另一个接口。
答案 0 :(得分:0)
将新项目添加到数组中时,只需执行以下操作:
var add:Ixyz={
_id: '',
item:'',
title: this.books[1]
}
这将提供一个与填充下拉列表相匹配的默认值。