我有一个带有动态选项的"mat-select"标签(使用http加载(。
<mat-select placeholder="Parent" formControlName="ParentId">
<mat-option value="0">Without Parent</mat-option>
<mat-option [value]="item.id" *ngFor="let item of menuItems">{{item.title}}</mat-option>
</mat-select>
获取菜单项后,我setValue
表单:
async loadFormData() {
this.menuItems = await this.menuService.getMenuItems();
if (this.itemId) {
let item = await this.menuService.getMenuItem(this.itemId);
this.addMenuItemForm.setValue({ParentId: 13});
}
}
但它不起作用。 相同的代码可以完美地与静态选项一起使用。
setvalue
仅适用于控件。 将 patchvalue
用于表单。参考: https://angular.io/guide/reactive-forms#patching-the-model-value