【发布时间】:2016-07-30 22:15:04
【问题描述】:
我似乎不知道如何轻松地将一个值设置为选择下拉列表的默认值。
我当前的代码
<select class="form-control" ngControl="modID" #modID="ngForm">
<option *ngFor="let module of modules" [value]="module._id">{{module.moduleName}}</option>
</select>
我尝试过使用 [selected] 装饰器,但不知道如何让它工作。
假设我在下面有这个 modInst json 对象:
{
"_id": 5,
"semester": "Tri 3",
"year": 2018,
"__v": 0,
"modID": [
{
"_id": 6,
"moduleLeader": "Jake Groves",
"moduleName": "Software Implementation",
"__v": 0
}
]
},
我希望从所有模块中选择 modInst.modID[0]._id。_id(模块)是填充选择列表的内容
有什么简单的方法吗?
编辑:
我尝试添加[selected]="module._id == modInst.modID[0]._id",但我没有成功将其设为选定值
我也试过[ngValue]="modInst.modID[0]._id",但它仍然没有选择列表中id为6的模块
最后一个添加...我尝试了手动“选择”,但加载时仍然没有选择值[selected]="module._id == '7'"
【问题讨论】:
标签: angular