我究竟做错了什么? Angular mat-form-field
What am I doing wrong? Angular mat-form-field
我在网站控制台上遇到了这个错误。在构建应用程序时我没有遇到任何错误:
core.js:6210 ERROR Error: mat-form-field must contain a MatFormFieldControl.
at getMatFormFieldMissingControlError (form-field.js:227)
at MatFormField._validateControlChild (form-field.js:712)
at MatFormField.ngAfterContentInit (form-field.js:519)
at callHook (core.js:2573)
at callHooks (core.js:2542)
at executeInitAndCheckHooks (core.js:2493)
at refreshView (core.js:9521)
at refreshEmbeddedViews (core.js:10605)
at refreshView (core.js:9504)
at refreshComponent (core.js:10651)
我的html:
<form [formGroup]="rowForm">
<div formArrayName="rows">
<div *ngFor="let rows of getControls(); let i=index" [formGroupName]="i">
<mat-checkbox class="example-margin" formControlName="checkbox2" name="checkbox2" (click)="toggleInput()">Show hidden option!</mat-checkbox>
<mat-form-field class="example-full-width">
<input matInput placeholder="Input1" formControlName="input1" name="input1" required>
</mat-form-field>
<mat-form-field class="example-full-width">
<input matInput placeholder="Input2" formControlName="input2" name="input2" required>
</mat-form-field>
<mat-form-field class="example-full-width">
<input matInput placeholder="Input3" *ngIf="showInput" formControlName="input3" name="input3" required>
</mat-form-field>
<button mat-button color="primary" *ngIf="rowForm.controls.rows.controls.length > 1" (click)="deleteRow(i)" class="btn btn-danger">Delete Button</button>
</div>
</div>
<button mat-button color="primary" (click)="addNewRow()" class="btn btn-primary">Add new Row</button><br>
</form>
我的模块文件:
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { RegisterFormComponent } from './components/register-form/register-form.component';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { MatCardModule } from '@angular/material/card';
import { MatButtonModule } from '@angular/material/button';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { NgSelectModule } from '@ng-select/ng-select';
import { MatCheckboxModule } from '@angular/material/checkbox';
import { RowComponent } from './components/row/row.component';
@NgModule({
declarations: [
AppComponent,
RegisterFormComponent,
RowComponent
],
imports: [
BrowserModule,
AppRoutingModule,
MatCardModule,
MatButtonModule,
BrowserAnimationsModule,
MatFormFieldModule,
MatInputModule,
FormsModule,
ReactiveFormsModule,
NgSelectModule,
MatCheckboxModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
我做错了什么?提前致谢!
不重要
我的问题的最小长度的更多原因 - abcdefghijklmnoprstuwxyz - abcdefghijklmnoprstuwxyz - abcdefghijklmnoprstuwxyz - abcdefghijklmnoprstuwxyz
我想你忘了导入输入模块
import { MatInputModule } from '@angular/material/input';
MatInput 扩展自 MatFormFieldControl
错误是因为你有一个mat-form-field,但是里面有no a input/select...
你有
//WRONG
<mat-form-field class="example-full-width">
<input matInput placeholder="Input3"
*ngIf="showInput" formControlName="input3" name="input3" required>
</mat-form-field>
你应该把影响的 *ngIf
放到所有的 mat-form-field
//OK
<mat-form-field *ngIf="showInput" class="example-full-width">
<input matInput placeholder="Input3" formControlName="input3" name="input3" required>
</mat-form-field>
我在网站控制台上遇到了这个错误。在构建应用程序时我没有遇到任何错误:
core.js:6210 ERROR Error: mat-form-field must contain a MatFormFieldControl.
at getMatFormFieldMissingControlError (form-field.js:227)
at MatFormField._validateControlChild (form-field.js:712)
at MatFormField.ngAfterContentInit (form-field.js:519)
at callHook (core.js:2573)
at callHooks (core.js:2542)
at executeInitAndCheckHooks (core.js:2493)
at refreshView (core.js:9521)
at refreshEmbeddedViews (core.js:10605)
at refreshView (core.js:9504)
at refreshComponent (core.js:10651)
我的html:
<form [formGroup]="rowForm">
<div formArrayName="rows">
<div *ngFor="let rows of getControls(); let i=index" [formGroupName]="i">
<mat-checkbox class="example-margin" formControlName="checkbox2" name="checkbox2" (click)="toggleInput()">Show hidden option!</mat-checkbox>
<mat-form-field class="example-full-width">
<input matInput placeholder="Input1" formControlName="input1" name="input1" required>
</mat-form-field>
<mat-form-field class="example-full-width">
<input matInput placeholder="Input2" formControlName="input2" name="input2" required>
</mat-form-field>
<mat-form-field class="example-full-width">
<input matInput placeholder="Input3" *ngIf="showInput" formControlName="input3" name="input3" required>
</mat-form-field>
<button mat-button color="primary" *ngIf="rowForm.controls.rows.controls.length > 1" (click)="deleteRow(i)" class="btn btn-danger">Delete Button</button>
</div>
</div>
<button mat-button color="primary" (click)="addNewRow()" class="btn btn-primary">Add new Row</button><br>
</form>
我的模块文件:
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { RegisterFormComponent } from './components/register-form/register-form.component';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { MatCardModule } from '@angular/material/card';
import { MatButtonModule } from '@angular/material/button';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { NgSelectModule } from '@ng-select/ng-select';
import { MatCheckboxModule } from '@angular/material/checkbox';
import { RowComponent } from './components/row/row.component';
@NgModule({
declarations: [
AppComponent,
RegisterFormComponent,
RowComponent
],
imports: [
BrowserModule,
AppRoutingModule,
MatCardModule,
MatButtonModule,
BrowserAnimationsModule,
MatFormFieldModule,
MatInputModule,
FormsModule,
ReactiveFormsModule,
NgSelectModule,
MatCheckboxModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
我做错了什么?提前致谢!
不重要 我的问题的最小长度的更多原因 - abcdefghijklmnoprstuwxyz - abcdefghijklmnoprstuwxyz - abcdefghijklmnoprstuwxyz - abcdefghijklmnoprstuwxyz
我想你忘了导入输入模块
import { MatInputModule } from '@angular/material/input';
MatInput 扩展自 MatFormFieldControl
错误是因为你有一个mat-form-field,但是里面有no a input/select...
你有
//WRONG
<mat-form-field class="example-full-width">
<input matInput placeholder="Input3"
*ngIf="showInput" formControlName="input3" name="input3" required>
</mat-form-field>
你应该把影响的 *ngIf
放到所有的 mat-form-field
//OK
<mat-form-field *ngIf="showInput" class="example-full-width">
<input matInput placeholder="Input3" formControlName="input3" name="input3" required>
</mat-form-field>