要设置Angular Material中的select的默认值,可以使用FormControl和FormGroup来实现。
首先,创建一个FormGroup对象,并在其中创建一个FormControl对象来存储select的值。
import { Component } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
@Component({
selector: 'app-select-example',
templateUrl: './select-example.component.html',
styleUrls: ['./select-example.component.css']
})
export class SelectExampleComponent {
form: FormGroup;
constructor() {
this.form = new FormGroup({
selectValue: new FormControl('default value', Validators.required)
});
}
}
然后,在HTML模板中使用formControlName指令来绑定select的值,并使用ngModel来指定默认值。
在这个例子中,我们将"Default Value"设置为默认选项,并将其绑定到FormControl对象的初始值上。
请注意,要使用这种方法,您需要导入FormsModule和ReactiveFormsModule模块,并将其添加到您的模块的imports数组中。
这样,当组件加载时,默认值将自动设置为FormControl的值,并且可以使用ngModel来获取和设置select的值。