我们正试图在我们公司建立我们自己的形式场组件。我们试图这样包装材料设计的组件:

字段:

<mat-form-field>
    <ng-content></ng-content>
    <mat-hint align="start"><strong>{{hint}}</strong> </mat-hint>
    <mat-hint align="end">{{message.value.length}} / 256</mat-hint>
    <mat-error>This field is required</mat-error>
</mat-form-field>

文本框:

<field hint="hint">
    <input matInput 
    [placeholder]="placeholder" 
    [value]="value"
    (change)="onChange($event)" 
    (keydown)="onKeydown($event)" 
    (keyup)="onKeyup($event)" 
    (keypress)="onKeypress($event)">
</field>

用法:

<textbox value="test" hint="my hint"></textbox>

结果大致如下:

<textbox  placeholder="Personnummer/samordningsnummer" value="" ng-reflect-placeholder="Personnummer/samordningsnummer">
  <field>
    <mat-form-field class="mat-input-container mat-form-field">
      <div class="mat-input-wrapper mat-form-field-wrapper">
        <div class="mat-input-flex mat-form-field-flex">
          <div class="mat-input-infix mat-form-field-infix">
            <input _ngcontent-c4="" class="mat-input-element mat-form-field-autofill-control" matinput="" ng-reflect-placeholder="Personnummer/samordningsnummer" ng-reflect-value="" id="mat-input-2" placeholder="Personnummer/samordningsnummer" aria-invalid="false">
            <span class="mat-input-placeholder-wrapper mat-form-field-placeholder-wrapper"></span>
          </div>
        </div>
        <div class="mat-input-underline mat-form-field-underline">
          <span class="mat-input-ripple mat-form-field-ripple"></span>
        </div>
        <div class="mat-input-subscript-wrapper mat-form-field-subscript-wrapper"></div>
      </div>
    </mat-form-field>
  </field>
</textbox>

但是我在控制台得到“mat-form-field必须包含MatFormFieldControl”。我猜这与mat-form-field不直接包含matinput字段有关。但它包含了它,只是在ng-content投影中。

以下是闪电战: https://stackblitz.com/edit/angular-xpvwzf


当前回答

问题1:MatInputModule未导入

在模块中导入MatInputModule和MatFormFieldModule,即app.module.ts

import { MatInputModule } from '@angular/material/input';
import { MatFormFieldModule } from "@angular/material/form-field";

@NgModule({
    imports: [
        MatFormFieldModule,
        MatInputModule
    ]
})
export class AppModule { }

问题2:matInput -拼写错误/忘记添加

一定要添加matInput,它是区分大小写的。

<mat-form-field>
    <input matInput type="text" />
</mat-form-field>

问题3:无效的*ngIf放置

检查input标签上是否有任何条件在任何情况下都会为假,因为mat-form-field会在其中查找matInput。相反,把*ngIf放在mat-form-field标签上。

<mat-form-field>
   <input matInput *ngIf="someCondition"> // don't add condition here, else add in mat-form-field tag
</mat-form-field>

感谢@William Herrmann指出了这个问题#3

问题4:编译器仍然给出错误

如果angular编译器在修复上述问题后仍然报错,那么你必须尝试重新启动应用程序。

ng serve

其他回答

我收到了同样的错误消息,但在我的情况下,上述内容都不能解决问题。解决方案就在“标签”中。你需要添加'mat-label'和把你的输入'label'标签也。我的问题的解决方案是在下面的片段:

  <mat-label>
     Username
  </mat-label>
  <label>
    <input 
       matInput 
       type="text" 
       placeholder="Your username" 
       formControlName="username"/>
  </label>

这里引用官方文件:

错误:mat-form-field必须包含MatFormFieldControl 当您没有将表单字段控件添加到表单字段时,将发生此错误。如果你的表单字段包含一个原生的<input>或<textarea>元素,确保你已经添加了matInput指令,并导入了MatInputModule。其他可以充当表单字段控件的组件包括<mat-select>、<mat-chip-list>以及您已创建的任何自定义表单字段控件。

在这里了解更多关于创建“自定义表单字段控件”的信息

在组件中使用提供者。ts文件

@Component({
 selector: 'your-selector',
 templateUrl: 'template.html',
 styleUrls: ['style.css'],
 providers: [
 { provide: MatFormFieldControl, useExisting: FormFieldCustomControlExample }   
]
})

如果将mat from field中的mat滑动作为唯一的元素,也会发生相同的错误 在我的情况下,我有

 <mat-form-field>
    <mat-slide-toggle [(ngModel)]="myvar">
       Some Text
     </mat-slide-toggle>
 </mat-form-field>

如果<mat-form-field>中有几个属性,就可能发生这种情况 简单的解决方案是将滑动开关移动到根目录

如果mat-form-field下有input标签,那么每个输入都必须有matInput