如何使用 patchValue 将值数组传递到一个多选列表视图中

How to pass array of values into one multi selection list view using patchValue

我面临一个问题,即使用反应形式的 patchvalue 方法将值数组传递到多选列表 (angular material)。

输入数组

list2=["BLUEBERRY","GRAPES","APPLE"];// the value that need to be selected in the list

列表中存在的元素(通过遍历数组list1)

list1 = ["PINEAPPLE","PAPAYA","BLUEBERRY","GRAPES","APPLE"];

多选列表

<mat-form-field>  
<mat-select class="add_input" formControlName="menu"  placeholder="Available Menus" multiple>
  <mat-option *ngFor="let menus of list1" [value]="menus">{{menus}}</mat-option>
</mat-select> 

补丁值函数为

this.form.patchValue(
    {
      location:this.gps,
      menu:  <-- how to pass the list2 element here(direct reference is not working)

    }
  )

仅供参考 - 反应式表单已使用 FormBuilder

初始化

试试这个:

this.form.patchValue({
  location:this.gps,
  menu: [...this.list2]
})