mat-form-field必须包含MatFormFieldControl

Vik*_*son 129 angular-material2 angular

我们正在努力在公司建立自己的表格 - 组件.我们试图像这样包装材料设计的组件:

领域:

<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>
Run Code Online (Sandbox Code Playgroud)

文本框:

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

用法:

<textbox value="test" hint="my hint"></textbox>
Run Code Online (Sandbox Code Playgroud)

这导致大致如下:

    <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>
Run Code Online (Sandbox Code Playgroud)

但是我们在控制台中得到"mat-form-field必须包含MatFormFieldControl".我想这与mat-form-field没有直接包含matInput-field有关.但它包含它,它只是与ng-content投影有关.

这是一个闪电战:https: //stackblitz.com/edit/angular-xpvwzf

dar*_*ong 330

我有这个问题.我MatFormFieldModule在我的主模块导入,但忘了添加MatInputModuleimports数组,如下所示:

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

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

希望能帮助到你.

更多信息在这里.

  • 有一天我终于会想起这件事…… (27认同)
  • 正确的路径是 - import { MatInputModule } from '@angular/material/input'; (2认同)

Was*_*siF 59

解决方案1

进口MatInputModule里面app.module.ts

import { MatInputModule } from '@angular/material';

@NgModule({
  imports: [
     // .......
     MatInputModule
     // .......
  ]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

解决方案2

检查拼写matInput,确保它区分大小写

<input matInput type="text" />
Run Code Online (Sandbox Code Playgroud)

  • 就我而言,这是问题 1 和问题 3 :) 我们应该记住在导入新模块后重新启动应用程序。 (9认同)
  • 啊,这让我发疯了。原因之一为我工作。我觉得应该在文档中指定,即使现在看起来很明显。 (4认同)
  • 问题3.必须重新启动。谢谢! (4认同)
  • 正是我的问题没有插入“matInput”。:'( 谢谢 (3认同)
  • 问题 3. `&lt;mat-form-field&gt;` 的内容为空。这意味着您可以将“NgIf”与“&lt;input&gt;”一起使用,但您需要保证输入标签在任何情况下都存在。 (3认同)
  • 我遇到了问题 2,我从来没有觉得自己像个白痴。谢谢!:)) (2认同)

Min*_*ina 15

如果有人在尝试嵌套 a 后遇到此错误<mat-checkbox>,请加油!它在<mat-form-field>标签内不起作用。

  • 的确。有关更多详细信息,请参阅 /sf/answers/3812271011/。 (2认同)

use*_*158 14

从官方文档引用在这里:

错误:mat-form-field必须包含MatFormFieldControl

如果尚未将表单字段控件添加到表单字段,则会发生此错误.如果表单字段包含本机或元素,请确保已向其添加了matInput指令并已导入MatInputModule.可以充当表单字段控件的其他组件包括,和您创建的任何自定义表单字段控件.


n.y*_*nko 13

不幸的是,还不支持将内容投影到mat-form-field中.请跟踪以下github问题以获取有关它的最新消息.

到目前为止,唯一的解决方案是将内容直接放入mat-form-field组件或实现MatFormFieldControl类,从而创建自定义表单字段组件.

  • 我在谷歌上搜索了一条错误信息.[答案如下](/sf/answers/3275685731/),说明需要导入`MatInputModule`解决了我的问题.由于这是接受的答案,我在这里添加链接,希望它能节省其他时间. (86认同)
  • 我的问题是我忽略了导入“ ** FormsModule **”。我希望这可以帮助别人。 (3认同)
  • 同样,在`&lt;mat-form-field&gt;`标签内,您必须确保在每个`input`标签上正确添加了`matInput`属性,或者在每个`&lt;select&gt;`上正确添加了`matNativeControl`属性。 (3认同)
  • @CGFoX 我希望他们能用这些信息和更好的例子更新文档...... (3认同)

小智 11

import {MatInputModule} from '@angular/material/input';



@NgModule({
    imports: [
        MatInputModule
    ],
    exports: [
        MatInputModule
    ]
})
Run Code Online (Sandbox Code Playgroud)

  • 欢迎来到 stackoverflow。回答问题时,请添加一些解释,说明您的代码片段的作用以及为什么它解决了OP的问题。欲了解更多信息,请查看此处[答案] (7认同)

小智 9

如果有人试图<mat-radio-group><mat-form-field> 下面嵌套一个像下面这样的,你会得到这个错误

         <mat-form-field>
                <!-- <mat-label>Image Position</mat-label> -->
                <mat-radio-group aria-label="Image Position" [(ngModel)]="section.field_1">
                    <mat-radio-button value="left">Left</mat-radio-button>
                    <mat-radio-button value="right">Right</mat-radio-button>
                </mat-radio-group>
            </mat-form-field>
Run Code Online (Sandbox Code Playgroud)

删除父 <mat-form-field>标签


小智 9

这意味着您没有像这样导入 MatInputModule

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

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


sev*_*ven 8

我也有这个问题,<mat-select>我的模板中有元素,当我将 MatSelectModule 导入模块时,它可以工作,它不会产生科学,但仍然希望它可以帮助你。

import { MatSelectModule } from '@angular/material/select';

imports: [
    BrowserModule,
    BrowserAnimationsModule,
    MatSidenavModule,
    MatButtonModule,
    MatIconModule,
    MatToolbarModule,
    MatFormFieldModule,
    MatProgressSpinnerModule,
    MatInputModule,
    MatCardModule,
    MatSelectModule
],

<div class="example-container">
    <mat-form-field>
        <input matInput placeholder="Input">
    </mat-form-field>

    <mat-form-field>
        <textarea matInput placeholder="Textarea"></textarea>
    </mat-form-field>

    <mat-form-field>
        <mat-select placeholder="Select">
            <mat-option value="option">Option</mat-option>
        </mat-select>
    </mat-form-field>
</div>
Run Code Online (Sandbox Code Playgroud)


Ale*_*xei 8

如果您在mat-form-field内输入了适当的内容,但也有输入,也会发生这种情况ngIf。例如:

<mat-form-field>
    <mat-chip-list *ngIf="!_dataLoading">
        <!-- other content here -->
    </mat-chip-list>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)

就我而言,mat-chip-list应该仅在数据加载后才“出现”。但是,执行了验证并且mat-form-field抱怨

mat-form-field必须包含MatFormFieldControl

要修复它,控件必须存在,所以我使用了[hidden]

<mat-form-field>
    <mat-chip-list [hidden]="_dataLoading">
        <!-- other content here -->
    </mat-chip-list>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)

另一种解决方案是提出Mosta:移动* ngIf为mat-form-field

<mat-form-field *ngIf="!_dataLoading">
    <mat-chip-list >
        <!-- other content here -->
    </mat-chip-list>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,而不是使用隐藏,您可以在整个mat-form-field标签上移动ngIf条件 (2认同)

小智 6

我不确定它是否可以这么简单,但我有同样的问题,在输入字段中将"mat-input"更改为"matInput"解决了问题.在你的情况下,我看到"matinput",它导致我的应用程序抛出相同的错误.

<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">
Run Code Online (Sandbox Code Playgroud)

"matinput"

在此输入图像描述

"matInput"

在此输入图像描述

  • 它发生在我身上,我忘了将`mdInput`改为`matInput`. (2认同)
  • Angular 5:<input>标签属性应该是`matInput`而不是`mat-input`. (2认同)

7gu*_*uyo 6

角度 9+ ...材料 9+

我注意到 3 个错误可能会导致相同的错误:

  1. 确保您已在导入组件的 app.module 或 module.ts 中导入 MatFormFieldModule 和 MatInputModule(如果是嵌套模块)
  2. 当您将材质按钮或复选框包装在 mat-form-field 中时。查看可以用mat -form-field 包裹的材质组件列表
  3. 当您未能在标记中包含 matInput 时。即<input matInput type =“number”step =“0.01”formControlName =“price”/>


Cyr*_*ris 6

我在一项 Karma 测试中遇到了问题

正如大多数答案已经指出的那样,丢失的模块可能会导致问题,并且也必须在 Karma TestBed 中重新导入它们。另外,我们似乎还需要导入 BrowserAnimationsModule 才能使一切正常工作。

在下面的代码中,我注释了一些可能对您没有用的模块,但其他 4 个导入肯定会有所帮助!

beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ EventFormComponent ],
      imports: [
        # HttpClientTestingModule,
        # RouterTestingModule,
        ReactiveFormsModule,
        MatFormFieldModule,
        MatInputModule,
        BrowserAnimationsModule,
        # TranslateModule.forRoot(),
      ],
    })
    .compileComponents();
  }));
Run Code Online (Sandbox Code Playgroud)


cjn*_*cjn 5

如果上面的所有主要代码结构/配置答案都不能解决您的问题,那么还有一件事要检查:确保您没有以某种方式使您的<input>标签无效。

例如,我mat-form-field must contain a MatFormFieldControl在不小心将required属性放在自关闭斜杠之后后收到了相同的错误消息/,这实际上使我的<input/>. 换句话说,我是这样做的(见输入标签属性的结尾):

错误

<input matInput type="text" name="linkUrl" [formControl]="listingForm.controls['linkUrl']" /required>
Run Code Online (Sandbox Code Playgroud)

<input matInput type="text" name="linkUrl" [formControl]="listingForm.controls['linkUrl']" required/>

Run Code Online (Sandbox Code Playgroud)

如果您犯了那个错误或其他使您的 无效<input>mat-form-field must contain a MatFormFieldControl错误,那么您可能会收到错误消息。无论如何,这只是您在调试时要寻找的另一件事。