您可以使用FormGroup作为mat-select的值,并使用FormControl来绑定mat-select的选中值。下面是一个示例代码:
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
@Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.css']
})
export class MyComponentComponent implements OnInit {
myForm: FormGroup;
options = [
{ value: 'option1', label: 'Option 1' },
{ value: 'option2', label: 'Option 2' },
{ value: 'option3', label: 'Option 3' }
];
selectedOption: string;
ngOnInit() {
this.myForm = new FormGroup({
selectedOption: new FormControl('')
});
}
}
在上面的代码中,我们创建了一个FormGroup,并使用FormControl来绑定mat-select的选中值。在ngOnInit方法中,我们初始化了myForm并设置了selectedOption的初始值为空字符串。然后,在模板中,我们使用formControlName指定了mat-select绑定的表单控件,使用[(value)]来设置mat-select当前选中的值。
这样,您就可以将FormGroup作为mat-select的值,并使用FormControl来处理选中的值。