我正在使用表单控件,这是我的html组件的代码
<mat-form-field>
<mat-select placeholder="Toppings" [formControl]="toppings" multiple>
<mat-option *ngFor="let topping of toppingList" [value]="topping">{{topping.value}}</mat-option>
</mat-select>
</mat-form-field>Run Code Online (Sandbox Code Playgroud)
我的ts文件是
export class SelectMultipleExample {
toppings = new FormControl();
toppingList: any[] = [
{ id:1,value:"test 1"},
{ id:2,value:"test 2"},
{ id:3,value:"test 4"},
{ id:4,value:"test 5"},
{ id:5,value:"test 6"},
{ id:6,value:"test 7"}
];
constructor(){
this.bindData();
}
bindData(){
const anotherList:any[]=[
{ id:1,value:"test 1"},
{ id:2,value:"test 2"}
]
this.toppings.setValue(anotherList)
}
}Run Code Online (Sandbox Code Playgroud)
我想为mat select设置默认值,如何实现这一点的任何帮助都会很棒。我想设置多个默认值。
我使用了 angular material(" @angular/material": "7.1.0 ") mat-select box 然后我使用了表单控件而不是 ng 模型,现在的问题是我无法在加载组件时设置该值。我需要将列表中的第一个值设置为 mat-select 框,我试过了,但我不能这样做。
我在 stackblitz 创建了一个代码,这里是链接:https ://stackblitz.com/edit/angular-zt1a9f
请帮助我。