Angular 4 mat-form-field,带有mat-select

Joh*_*ord 6 angular-material2 angular angular4-forms

所以,我在mat-form-field中使用mat-select时遇到了问题.使用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>
Run Code Online (Sandbox Code Playgroud)

我的视图模块包含此组件并导入到我的主app.module.ts文件中,如下所示:

...
import { MatFormFieldModule, MatInputModule, MatSelectModule } from 
   '@angular/material';
...

@NgModule({
  imports: [
    ...
    MatInputModule,
    MatFormFieldModule,
    MatSelectModule,
    ...
  ],
})
export class ViewModule {}
Run Code Online (Sandbox Code Playgroud)

我的主app.module.ts文件包含ViewModule和NoConflictStyleCompatibilityMode导入,如下所示:

...
import { ViewModule } from './view/view.module';
import { NoConflictStyleCompatibilityMode } from '@angular/material';
...
@NgModule({
  ...
  imports: [
    ViewModule,
    NoConflictStyleCompatibilityMode
  ],
  ...
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

当我从mat-select周围移除mat-form-field时,错误消失了,但是我对mat-input(使用mat-form-field)和mat-select的样式不一致.我正在导入MatSelectModule和MatFormFieldModule,但我收到此错误.我还更新了我的npm角度材料2,以便它具有最新和最好的,但仍然没有.我在俯瞰什么?我已经看到在最近的stackoverflows中解决了这种类型的问题,但我已经尝试过的每个解决方案都没有运气.

mat-select不正常工作
mat-form-field必须包含MatFormFieldControl

skm*_*skm 6

我遇到了类似的问题,从github 上的这个问题我意识到 ngIf 不能在 mat-select 上。

这可能对某些人有所帮助:

确保您的 mat-select 上没有 ngIf。


Gre*_*ake 2

尝试仅matInput向嵌套添加属性mat-select(如错误消息所示):

<mat-form-field>
    <mat-select matInput name="mySelect">
        <mat-option>
            ...
        </mat-option>
    </mat-select>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)

这个结构对我有用。

UPD.:快速 plnkr 示例https://plnkr.co/edit/9Yz6xkcVMCMpRhP3Qse9