【问题标题】:Getting ERROR mat-form-field must contain a MatFormFieldControl获取 ERROR mat-form-field 必须包含 MatFormFieldControl
【发布时间】:2019-05-29 00:14:48
【问题描述】:

我正在使用 Angular 表单来验证反应式表单。 对于模板,我在我的项目中使用材料设计,

在文本框中输入条件 ngIf 进行验证时出现错误

error is -: ERROR 错误:mat-form-field 必须包含一个 MatFormFieldControl。

请看-:

为了消除这个问题,我已经 1.在module.ts文件中导入MatInputModule 2. 已经添加 必须在输入中添加matInput

但还是报同样的错误

我在下面放代码

模板-:

<form class="example-form" novalidate (ngSubmit)='user_signup(user)'  [formGroup]='user'>

          <div class="row align-items-center">
            <div class="col-md-1">
              <label><img src="assets/imgs/mobile-icon.svg"/></label>
            </div>
            <div class="col-md-11">
              <mat-form-field class="example-full-width" >
                <input matInput type='number' placeholder="Phone Number:"  maxlength="10" name="phone" formControlName="phone" *ngIf="( user.get('phone').hasError('required') || user.get('phone').hasError('minlength') || user.get('phone').hasError('maxlength'))&& user.get('phone').touched" required/>
              </mat-form-field>
              <div>
              <div class="error" *ngIf="user.get('phone').hasError('required') && user.get('phone').touched">
                Phone number Required
              </div>
              <div class="error" *ngIf="user.get('phone').hasError('minlength') && user.get('phone').touched">
                Min 10 digit
              </div>
              <div class="error" *ngIf="user.get('phone').hasError('maxlength') && user.get('phone').touched">
                Max 10 digit
              </div>
              </div>

            </div>
          </div>
</form>

component.ts

 import { Component, OnInit } from '@angular/core';
    import { ServicesService } from '../service/services.service';
    import { FormGroup  , FormControl  , Validators } from '@angular/forms';
    @Component({
      selector: 'app-register',
      templateUrl: './register.component.html',
      styleUrls: ['./register.component.scss']
    })
    export class RegisterComponent implements OnInit {
     user: FormGroup;
 constructor( public restapi:ServicesService) {

        this.user = new FormGroup({
          password: new FormControl('', [Validators.required, Validators.minLength(6)]),
          email: new FormControl('', [Validators.required,Validators.email]),
          phone: new FormControl('', [Validators.required, Validators.minLength(10),Validators.maxLength(10)]),
          });

       }

  ngOnInit() {
  }
}

模块.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule, NO_ERRORS_SCHEMA } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { ComponentsComponent } from './components/components.component';
import { ReactiveFormsModule } from '@angular/forms';
import { HttpClientModule }    from '@angular/common/http';


import { MDBBootstrapModule, WavesModule, ButtonsModule, CardsFreeModule } from 'angular-bootstrap-md';
import { MatTabsModule, MatDialogModule } from '@angular/material';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatFormFieldModule} from '@angular/material';


/*angular material compoment*/
import { MatInputModule } from '@angular/material/input';
import {MatButtonModule} from '@angular/material/button';
import { RouterModule, Routes } from '@angular/router';
import {MatSlideToggleModule} from '@angular/material/slide-toggle';
import {MatTableModule} from '@angular/material/table';
import {MatSelectModule} from '@angular/material/select';
import {MatExpansionModule} from '@angular/material/expansion';
import {MatProgressSpinnerModule} from '@angular/material/progress-spinner';
import {MatSnackBarModule} from '@angular/material/snack-bar';
import {MatSliderModule} from '@angular/material/slider';
import {MatBadgeModule} from '@angular/material/badge';

/*component */
import { LoginComponent } from './login/login.component';
import { RegisterComponent } from './register/register.component';
import { FleshScreenComponent } from './flesh-screen/flesh-screen.component';


/* Service */
import { ServicesService } from './service/services.service';

@NgModule({
  declarations: [
    AppComponent,
    ComponentsComponent,
    LoginComponent,
    RegisterComponent,
    FleshScreenComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    BrowserAnimationsModule,
    MatInputModule,
    MatButtonModule,
    MDBBootstrapModule.forRoot(),
    ReactiveFormsModule ,
    HttpClientModule ,
    MatFormFieldModule

  ],
  schemas: [ NO_ERRORS_SCHEMA ],
  providers: [ServicesService],
  bootstrap: [AppComponent]
})
export class AppModule { }

【问题讨论】:

  • 您是否尝试过将 formControlName 移至 mat 表单字段?
  • 我还不能做到这一点
  • 请考虑不要从主入口点导入一些模块和从辅助入口点导入其他模块。考虑从主入口点导入所有 Material 模块,或从专用入口点导入模块。
  • @Edric 我听不懂,请您详细说明..
  • 例如,您从@angular/material 导入MatFormFieldModule。在同一代码的更下方,您将从@angular/material/input 导入MatInputModule,从@angular/material/button 导入MatButtonModule 等等。我建议你做的是从同一个根目录导入所有模块。 (因为您应该使用相同的导入而不是导入子路径入口点。)(例如,从@angular/material 导入MatButtonModule。)

标签: angular angular-material angular5 angular6 angular-reactive-forms


【解决方案1】:

我还没有尝试过,但您可能会因为*ngIf 指令应用于input 元素而收到此错误。

为什么?

特别是,在您的这部分代码中:

<mat-form-field>

    <input *ngIf="...">

</mat-form-field>

Angular 解释它as follows:

<mat-form-field>

    <ng-template [ngIf]="...">
      <input>
    </ng-template>

</mat-form-field>

所以在底层,input 元素不是mat-form-field 的直接内容子元素。目前没问题。

但是,在深入了解其source 之后,MatFormField 需要通过@ContentChild 找到您的input(应用了matInput 指令)。这意味着您必须提供 input 元素作为 mat-form-field 的内容子元素。

简而言之,您需要以某种方式将input 元素直接放在mat-form-field 元素之下。

解决方案

不要将*ngIf 应用于input 元素,而是将其应用于mat-form-field。这样一来,input 元素就是mat-form-field 的内容子元素,应该是这样。

<mat-form-field *ngIf="...">
    <input>
</mat-form-field>

【讨论】:

    【解决方案2】:

    当使用 ma​​t-form-field 标签和内部 input/select 标签时,标签必须包含 ma​​tNativeControl 属性。

    例如:

    <mat-form-field>
        <input matInput matNativeControl placeholder="Input">
      </mat-form-field>
    

    参考官方文档:Link

    【讨论】:

      【解决方案3】:

      好像有错别字,请把你的代码改成这个

       <mat-form-field class="example-full-width" >
                    <input matInput type='number' placeholder="Phone Number:"  maxlength="10" name="phone" formControlName="phone" *ngIf="user.get('phone').hasError('required') || user.get('phone').hasError('minlength') || user.get('phone').hasError('maxlength') && user.get('phone').touched" required/>
                  </mat-form-field>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-03-22
        • 2021-01-30
        • 2018-03-24
        • 2020-09-05
        • 2021-04-10
        • 1970-01-01
        • 2019-01-06
        相关资源
        最近更新 更多