【发布时间】: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