Angular material 加载具有多个选项并选中某些选项的材质选择?

Angular material 加载具有多个选项并选中某些选项的材质选择?,angular-material,Angular Material,我正在尝试使用多个选项(如)实现材质选择。但是我想根据api返回的数据填充页面加载上的某些复选框。我该怎么做?我在任何地方都找不到选中的或选中的属性。嗯,您有两种可能性: 在您的示例中,您使用的是被动表单,因此在Ngonit事件挂钩中,您可以设置formControl的值,如下所示: ngOnInit() { this.toppings.setValue(['Onion', 'Mushroom']) } // some html template <mat-form-fiel

我正在尝试使用多个选项(如)实现材质选择。但是我想根据api返回的数据填充页面加载上的某些复选框。我该怎么做?我在任何地方都找不到
选中的
选中的
属性。

嗯,您有两种可能性:

在您的示例中,您使用的是被动表单,因此在Ngonit事件挂钩中,您可以设置formControl的值,如下所示:

ngOnInit() {
    this.toppings.setValue(['Onion', 'Mushroom'])
  }
// some html template
<mat-form-field>
  <mat-label>Toppings</mat-label>
  <mat-select [(ngModel)]="selected" multiple>
    <mat-option  *ngFor="let topping of toppingList" [value]="topping">{{topping}}</mat-option>
  </mat-select>
</mat-form-field>
如果您更喜欢模板驱动的,而不是
[(值)]
请尝试
[(ngModel)]
,如下所示:

ngOnInit() {
    this.toppings.setValue(['Onion', 'Mushroom'])
  }
// some html template
<mat-form-field>
  <mat-label>Toppings</mat-label>
  <mat-select [(ngModel)]="selected" multiple>
    <mat-option  *ngFor="let topping of toppingList" [value]="topping">{{topping}}</mat-option>
  </mat-select>
</mat-form-field>
这里有一个建议,选择你的比萨饼:)