mat-form-field must contain a MatFormFieldControl

0 votes

I want to build a form-field-Components. I am trying to wrap the material design's Components like this:

Field:

<mat-form-field>

    <ng-content></ng-content>

    <mat-hint align="start"><strong>{{hint}}</strong> </mat-hint>

    <mat-hint align="end">{{message.value.length}} / 256</mat-hint>

    <mat-error>This field is required</mat-error>

</mat-form-field>

Textbox:

<field hint="hint">

    <input matInput 

    [placeholder]="placeholder" 

    [value]="value"

    (change)="onChange($event)" 

    (keydown)="onKeydown($event)" 

    (keyup)="onKeyup($event)" 

    (keypress)="onKeypress($event)">

</field>

Usage:

<textbox value="test" hint="my hint"></textbox>

The approximate result:

    <textbox placeholder="Personnummer/samordningsnummer" value="" ng-reflect-placeholder="Personnummer/samordningsnummer">

       <field>

          <mat-form-field class="mat-input-container mat-form-field>

             <div class="mat-input-wrapper mat-form-field-wrapper">

                <div class="mat-input-flex mat-form-field-flex">

                   <div class="mat-input-infix mat-form-field-infix">

                      <input _ngcontent-c4="" class="mat-input-element mat-form-field-autofill-control" matinput="" ng-reflect-placeholder="Personnummer/samordningsnummer" ng-reflect-value="" id="mat-input-2" placeholder="Personnummer/samordningsnummer" aria-invalid="false">

                      <span class="mat-input-placeholder-wrapper mat-form-field-placeholder-wrapper"></span>

                   </div>

                </div>

                <div class="mat-input-underline mat-form-field-underline">

                   <span class="mat-input-ripple mat-form-field-ripple"></span>

                </div>

                <div class="mat-input-subscript-wrapper mat-form-field-subscript-wrapper"></div>

             </div>

          </mat-form-field>

       </field>

    </textbox>

I am getting this error:  "mat-form-field must contain a MatFormFieldControl" in the console. How can I fix this?

May 13, 2022 in Angular by Kichu
• 19,040 points
3,770 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

To solve this, you have to Import MatInputModule in your module.ts file. 

import { MatInputModule } from '@angular/material/input';

The bad news is that content projection into mat-form-field is not supported yet. The one single thing you can do is to place your content directly into the mat-form-field component or implement a MatFormFieldControl class. This will create a custom form field.

answered May 16, 2022 by narikkadan
• 63,600 points

edited Mar 5
0 votes
You Probably forgot to add MatInputModule to you ngModule

Solution add

import {MatInputModule} from '@angular/material/input';
to your ngModule Imports.
answered Sep 8, 2022 by Orthodox

edited Mar 5

Related Questions In Angular

0 votes
1 answer

How to do a Jquery Callback after form submit?

Hello @kartik, Just do like this - $("#myform").bind('ajax:complete', function() ...READ MORE

answered Jul 24, 2020 in Angular by Niroj
• 82,840 points
8,388 views
0 votes
1 answer

How to send multipart/form-data for a file upload in Angular?

In Angular, you can send files using ...READ MORE

answered Feb 24 in Angular by Navya
126 views
0 votes
1 answer

How to create a mat table in Angular?

1. Install Angular Material ng add @angular/material 2. Import ...READ MORE

answered Mar 6 in Angular by Anvi
77 views
0 votes
1 answer

How can we reload a page after requesting the same get parameter using AngularJS?

hey kartik, The reason for this is that ...READ MORE

answered Feb 10, 2020 in Angular by Niroj
• 82,840 points
12,791 views
0 votes
1 answer

How to pass a string parameter from angular UI to node.js backend?

Hello Kartik, There are three ways to get ...READ MORE

answered Apr 22, 2020 in Angular by Niroj
• 82,840 points
11,123 views
0 votes
2 answers

How to detect a route change in Angular?

Hii Kartik For Angular 7 someone should write like: this.router.events.subscribe((event: Event) ...READ MORE

answered Apr 22, 2020 in Angular by Niroj
• 82,840 points
29,554 views
0 votes
1 answer

How to apply zoom animation for each element of a list in angular?

Hey @Sid, do check if this link ...READ MORE

answered Jul 30, 2019 in Others by Vardhan
• 13,150 points
1,827 views
0 votes
1 answer
0 votes
1 answer

Can not bind to 'formgroup' since it is not a known property of 'form'

In order to rectify this error, you ...READ MORE

answered Feb 10, 2022 in Others by Rahul
• 9,680 points
22,093 views
0 votes
1 answer

How to set meta tags using Angular universal SSR and ngx-seo plug-in?

first Install the plug-in with npm i ngx-seo ...READ MORE

answered Feb 11, 2022 in Others by narikkadan
• 63,600 points
2,396 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP