动态角度材料“选择”标签上的setValue不起作用

时间:2019-07-14 10:10:36

标签: angular angular-material

我有一个带有动态选项(通过http加载)的“ mat-select”标签。

    <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>

获取menuItem后,我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});
    }
  }

但不起作用。相同的代码与静态选项完美配合。

1 个答案:

答案 0 :(得分:1)

setvalue仅适用于控件。使用patchvalue进行表单。 参考:https://angular.io/guide/reactive-forms#patching-the-model-value

相关问题