要在FormGroup上正确使用Angular的
import { MatSelectModule } from '@angular/material/select';
import { ReactiveFormsModule } from '@angular/forms';
@NgModule({
imports: [
MatSelectModule,
ReactiveFormsModule
],
...
})
export class AppModule { }
import { FormBuilder, FormGroup } from '@angular/forms';
@Component({
...
})
export class YourComponent {
form: FormGroup;
constructor(private formBuilder: FormBuilder) {
this.form = this.formBuilder.group({
selectedOption: [''] // 默认选项
});
}
}
options = [
{ value: 'option1', label: '选项1' },
{ value: 'option2', label: '选项2' },
{ value: 'option3', label: '选项3' }
];
这样,