您可以使用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
来处理选中的值。