【发布时间】:2018-04-16 16:57:53
【问题描述】:
所以,我在使用 mat-select 和 mat-form-field 时遇到问题。将 mat-form-field 与 mat-input 一起使用不是问题,而且我相当确定我的导入也是正确的,但是在尝试使用 mat-select 时收到以下错误:
错误:md-form-field 必须包含 MdFormFieldControl。是不是忘了给原生添加 mdInput...
我的 HTML 代码如下:
<mat-form-field class="full-width">
<mat-select name="sampleType" formControlName="sampleTypeCtrl"
placeholder="Sample Type" required>
<mat-option *ngFor="let sample of samples" [value]="sample.value">
{{ sample.viewValue }}
</mat-option>
</mat-select>
</mat-form-field>
包含此组件并导入到我的主 app.module.ts 文件中的视图模块如下:
...
import { MatFormFieldModule, MatInputModule, MatSelectModule } from
'@angular/material';
...
@NgModule({
imports: [
...
MatInputModule,
MatFormFieldModule,
MatSelectModule,
...
],
})
export class ViewModule {}
我的主 app.module.ts 文件包含 ViewModule 和 NoConflictStyleCompatibilityMode 导入,如下所示:
...
import { ViewModule } from './view/view.module';
import { NoConflictStyleCompatibilityMode } from '@angular/material';
...
@NgModule({
...
imports: [
ViewModule,
NoConflictStyleCompatibilityMode
],
...
})
export class AppModule { }
当我从 mat-select 周围删除 mat-form-field 时,错误消失了,但是我发现 mat-input(使用 mat-form-field)和 mat-select 的样式不一致。我正在同时导入 MatSelectModule 和 MatFormFieldModule,但出现此错误。我还更新了我的角度材料 2 的 npm,以便它具有最新和最好的,但仍然没有。我在看什么?我在最近的 stackoverflows 中看到了此类问题,但我已经尝试过每个解决方案都没有运气。
mat-select Not Working Properly
mat-form-field must contain a MatFormFieldControl
【问题讨论】:
-
这些模块
MatInputModule, MatFormFieldModule, MatSelectModule存在于 ViewModule 中,但它们不是 AppModule 的一部分。您应该从 ViewModule 导出材质模块
标签: angular angular-material2 angular4-forms