在Angular 7中,如果使用[ngModel]和mat-select时遇到问题,可以按照以下步骤来解决:
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
@NgModule({
imports: [
FormsModule,
ReactiveFormsModule
],
// ...
})
export class AppModule { }
import { Component } from '@angular/core';
import { FormControl } from '@angular/forms';
@Component({
selector: 'app-component',
template: `
选择一个选项
{{ option }}
`
})
export class AppComponent {
selectedOption = new FormControl();
options = ['选项1', '选项2', '选项3'];
}
this.selectedOption.setValue('选项1');
import { MatSelectModule } from '@angular/material/select';
@NgModule({
imports: [
MatSelectModule
],
// ...
})
export class AppModule { }
通过遵循这些步骤,应该能够正确使用[ngModel]和mat-select。如果问题仍然存在,请检查浏览器控制台是否有任何错误消息,并确保Angular和Material版本兼容。