角度自动分配下拉列表



>我这里有一个下拉角度选择标签。赋值时不会选择下拉列表。

<select [(ngModel)]="controlType" #ctrl="ngModel" class="form-control" [ngModelOptions]="{standalone: true}">
<option [value]="undefined" selected>SILA PILIH</option>
<option *ngFor="let alertControl of alertControlList" [ngValue]="alertControl">{{ alertControl?.desc }}
</option>
</select>

控件从数据库键入

{
"errorCode": null,
"errorMessage": null,
"id": "b9e86e1a-cabc-11e8-9257-31ce15c52e16",
"createdBy": "a6ef4d81-744e-49e7-9e1d-ab74a14935d6",
"lastModifiedBy": "a6ef4d81-744e-49e7-9e1d-ab74a14935d6",
"createdDate": 1538977256000,
"lastModifiedDate": 1538977256000,
"deleted": null,
"active": true,
"version": 0,
"code": "1",
"desc": "why"
}

警报控件列表值

[{
"code": "2",
"version": "0",
"desc": "why",
"id": "b9e8bc3b-cabc-11e8-9257-31ce15c52e16",
"HEX(id)": "B9E8BC3BCABC11E8925731CE15C52E16"
}, {
"code": "1",
"version": "0",
"desc": "why",
"id": "b9e86e1a-cabc-11e8-9257-31ce15c52e16",
"HEX(id)": "B9E86E1ACABC11E8925731CE15C52E16"
}]

只有当我在这里进行对象映射时,它才有效.还有其他更简单的解决方案吗?

for (let int i = 0; i < alertControlList.length; i++) {
if (alertControlList[i].code === dropdown.code) {
console.log('do MATCH' + i + ' code' + dropdown.code);
controlType = alertControlList[i];
}
}

改用[selected]="alertControl.code === dropdown.code"

这不能直接工作,因为您将option值设置为对象,并且ngModel与该值不匹配,因此它不会显示为选中状态。相反。您似乎也同时使用双向绑定的ngModelngFormngModel。使用其中任何一个。如果使用ngForm获取表单值,则可以使用valueselect上设置值,并将option值更改为alertControl中的唯一属性,例如code

<select #ctrl="ngModel" [value]="controlType.code" class="form-control">
<option *ngFor="let alertControl of alertControlList" [value]="alertControl.code">{{ alertControl?.desc }}</option>
</select>

相关内容

最新更新