mat-form-field必须包含MatFormFieldControl并且已经导入

ole*_*ank 3 angular-material angular

我收到错误消息ERROR mat-form-field must contain a MatFormFieldControl,这应该意味着我需要在最近的父模块中导入MatFormFieldModule。但是我已经做了,所以我不明白问题出在什么地方...

模板

<form [formGroup]="form" (ngSubmit)="handleSubmit()">
    <mat-form-field>
        <input matInput placeholder="Name" formControlName="name" />
    </mat-form-field>

    <mat-form-field>
        <textarea
            matInput
            placeholder="Active Description"
            formControlName="activeDescription"
        ></textarea>
    </mat-form-field>

    <mat-form-field>
        <textarea
            matInput
            placeholder="Inactive Description"
            formControlName="inactiveDescription"
        ></textarea>
    </mat-form-field>

    <mat-form-field>
        <mat-checkbox formControlName="active">Active</mat-checkbox>
    </mat-form-field>
</form>
Run Code Online (Sandbox Code Playgroud)

模组

import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MatButtonModule, MatCheckboxModule, MatFormFieldModule, MatInputModule, MatSelectModule } from '@angular/material';
import { EffectsModule } from '@ngrx/effects';
import { StoreModule } from '@ngrx/store';
import { PaymentConfigSelectorComponent } from './payment-config-selector/payment-config-selector.component';
import { PaymentMethodComponent } from './payment-method/payment-method.component';
import { PaymentMethodsComponent } from './payment-methods/payment-methods.component';
import { PaymentRoutingModule } from './payment-routing.module';
import { PaymentEffects } from './payment.effects';
import { paymentReducer } from './payment.reducer';
import { PaymentSmartComponent } from './smart/payment-smart.component';

@NgModule({
  declarations: [PaymentSmartComponent, PaymentMethodsComponent, PaymentConfigSelectorComponent, PaymentMethodComponent ],
  imports: [
    CommonModule,
    PaymentRoutingModule,
    FormsModule,
    ReactiveFormsModule,
    EffectsModule.forFeature([PaymentEffects]),
    StoreModule.forFeature(
      'payment',
      paymentReducer,
    ),
    MatFormFieldModule, MatInputModule, MatSelectModule, MatCheckboxModule, MatButtonModule,
  ],
  providers: [
  ],
})
export class PaymentModule { }
Run Code Online (Sandbox Code Playgroud)

Raw*_*ode 16

我必须这样做才能让它发挥作用:-

<mat-form-field>
  <input hidden=true matInput> // this gets rid of the error, while the form still uses the input received in the
  mat-checkbox
  <mat-checkbox matInput formControlName="active">Active</mat-checkbox>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)


小智 12

我通过添加隐藏的输入字段解决了该问题。

<mat-form-field>
 <mat-label>My Label</mat-label>
 <input matInput [hidden]="true" [(ngModel)]="myModel" name="myModel">
 <mat-checkbox class="ml-3" [(ngModel)]="myModel" name="myModel"></mat-checkbox>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)


Cét*_*tia 9

实际上,该错误并不意味着模块导入问题,而是您在使用mat-form-field没有有效控件的情况。

问题在这里:

<mat-form-field>
    <mat-checkbox formControlName="active">Active</mat-checkbox>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)

MatChebox不应包含在中mat-form-field。Juste取出容器

  • 是的,它的名字很糟糕。它更像是一个`mat-form-text-field`,因为它是添加下划线+提示+浮动标签+错误...等的组件 (7认同)
  • mat-checkbox 使用 mat-form-field 是否有替代方案?因为我的所有表单字段都使用 mat-form-field(复选框除外),因此该字段与其他字段不对齐。 (4认同)
  • 谢谢,说得对。但我认为这没有任何意义,因为它确实是一个表单“字段”——或者至少是表单的一部分。但感谢您的解决方案。 (3认同)