首页 > 解决方案 > 动态角度材料“选择”标签上的 setValue 不起作用

问题描述

我有一个带有动态选项的“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>

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

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

标签: angularangular-material

解决方案


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


推荐阅读