Last updated: Apr 4, 2024
Reading timeยท4 min

The Angular error "mat-form-field must contain a MatFormFieldControl" occurs for multiple reasons:
MatInputModule.matInput directive to input and textarea
elements.*ngIf on an input or a textarea element.mat-form-field element.mat-form-field element in your code.Make sure to add the matInput directive to your input and textarea elements
and import MatInputModule to resolve the error.

Here is the complete stack trace.
ROR Error: mat-form-field must contain a MatFormFieldControl. at getMatFormFieldMissingControlError (form-field.mjs:430:12) at MatFormField._assertFormFieldControl (form-field.mjs:705:19) at MatFormField.ngAfterContentInit (form-field.mjs:606:14) at callHook (core.mjs:2487:22) at callHooks (core.mjs:2456:17) at executeInitAndCheckHooks (core.mjs:2407:9) at refreshView (core.mjs:10407:21) at refreshComponent (core.mjs:11427:13) at refreshChildComponents (core.mjs:10157:9) at refreshView (core.mjs:10416:13)
MatInputModule into your moduleOpen your app.module.ts file and make sure to import MatInputModule and add
it to your imports array.
import { MatFormFieldModule } from "@angular/material/form-field"; // ๐๏ธ import MatInputModule import { MatInputModule } from '@angular/material/input'; @NgModule({ imports: [ // ... your other imports MatFormFieldModule, // ๐๏ธ add it to your imports array MatInputModule ] }) export class AppModule { }
If you also use the mat-select element, make sure to import it as well.
import { MatFormFieldModule } from '@angular/material/form-field'; import { MatInputModule } from '@angular/material/input'; // ๐๏ธ import the `MatSelectModule` module as well import { MatSelectModule } from '@angular/material/select'; @NgModule({ imports: [ // ... your other imports MatFormFieldModule, // ๐๏ธ add MatInputModule to your imports array MatInputModule, // ๐๏ธ add MatSelectModule to your imports array MatSelectModule, ] }) export class AppModule { }

Try to restart your Angular server if the issue persists.
Ctrl + C to stop your dev server.npm start command.matInput directive to your input and textarea elementsMake sure you've added the matInput directive to your input and textarea
elements.
Here is a working example from my app.component.html file.
<mat-form-field appearance="fill"> <mat-label>Input</mat-label> <input matInput /> </mat-form-field> <mat-form-field appearance="fill"> <mat-label>Textarea</mat-label> <textarea matInput></textarea> </mat-form-field>

If your form field contains native input or textarea elements, you have to
set the matInput directive as shown in the code sample.
Make sure you haven't misspelled the directive as it is case-sensitive.
Check that you also haven't misspelled the names of any of the tags, e.g.
<Input /> (incorrect) vs <input /> (correct).
If you work with select elements, you should set the matNativeControl
directive instead, e.g. <select matNativeControl>.
Here is an example of using a mat-select tag.
<mat-form-field appearance="fill"> <mat-label>Select</mat-label> <mat-select> <mat-option value="one">First option</mat-option> <mat-option value="two">Second option</mat-option> </mat-select> </mat-form-field>

*ngIf on an Input or Textarea element.Make sure you haven't set *ngIf on an input or a textarea element if the
error persists.
For example, the following code causes the issue.
<!-- โ๏ธ Incorrectly setting *ngIf --> <mat-form-field appearance="fill"> <mat-label>Input</mat-label> <input matInput *ngIf="condition" /> </mat-form-field>
Instead, set *ngIf on the mat-form-field element.
<!-- โ Correctly setting *ngIf --> <mat-form-field *ngIf="this.magicNumber > 1" appearance="fill"> <mat-label>Input</mat-label> <input matInput /> </mat-form-field>
Try to restart your Angular server if the issue persists.
Ctrl + C to stop your dev server.npm start command.You can also run the npx ng serve command to start your Angular server.
npx ng serve
mat-checkbox inside a mat-form-fieldThe error also occurs if you try to use a mat-checkbox inside a
mat-form-field.
You can only use the following Angular Material components inside a
mat-form-field:
<input matNativeControl><textarea matNativeControl><select matNativeControl><mat-select><mat-chip-list>If you try to use any other Angular Material components inside mat-form-field,
the error occurs.
You can try to remove the mat-form-field and see if the error is resolved.
mat-form-field as an empty tagLeaving a mat-form-field tag as empty also causes the error.
<!-- โ๏ธ Error: mat-form-field must contain a MatFormFieldControl. --> <mat-form-field appearance="fill"> </mat-form-field>
Either remove the tag or add child elements to it.
To solve the Angular error "mat-form-field must contain a MatFormFieldControl", make sure:
MatInputModule and added it to your imports array.matInput directive on input and textarea elements.*ngIf on an input or a textarea element.mat-form-field element.mat-form-field elements in your code.You can learn more about the related topics by checking out the following tutorials: