mat-form-field must contain a MatFormFieldControl

前端 未结 30 1537
萌比男神i
萌比男神i 2020-11-28 04:35

We are trying to build our own form-field-Components at our Company. We are trying to wrap material design\'s Components like this:

field:



        
相关标签:
30条回答
  • 2020-11-28 04:43

    You can set appearance="fill" inside your mat-form-field tag, it works for me

    <form class="example-form">
      <mat-form-field class="example-full-width" appearance="fill">
        <mat-label>Username Or Email</mat-label>
        <input matInput placeholder="Username Or Email" type="text">
      </mat-form-field>
    
      <mat-form-field class="example-full-width" appearance="fill">
        <mat-label>Password</mat-label>
        <input matInput placeholder="Password" type="password">
      </mat-form-field>
    </form>
    
    0 讨论(0)
  • 2020-11-28 04:44
    import {MatInputModule} from '@angular/material/input';
    
    
    
    @NgModule({
        imports: [
            MatInputModule
        ],
        exports: [
            MatInputModule
        ]
    })
    
    0 讨论(0)
  • 2020-11-28 04:44

    New updated MatInput Module import is:

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

    As per Angular Materials API

    0 讨论(0)
  • 2020-11-28 04:46

    If all of the main code structure/configuration answers above don't solve your issue, here's one more thing to check: make sure that you haven't in some way invalidated your <input> tag.

    For instance, I received the same mat-form-field must contain a MatFormFieldControl error message after accidentally putting a required attribute after a self-closing slash /, which effectively invalidated my <input/>. In other words, I did this (see the end of the input tag attributes):

    wrong:

    <input matInput type="text" name="linkUrl" [formControl]="listingForm.controls['linkUrl']" /required>
    

    right:

    <input matInput type="text" name="linkUrl" [formControl]="listingForm.controls['linkUrl']" required/>
    
    

    If you make that mistake or something else to invalidate your <input>, then you could get the mat-form-field must contain a MatFormFieldControl error. Anyway, this is just one more thing to look for as you're debugging.

    0 讨论(0)
  • 2020-11-28 04:47

    I'm not sure if it could be this simple but I had the same issue, changing "mat-input" to "matInput" in the input field resolved the problem. In your case I see "matinput" and it's causing my app to throw the same error.

    <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">
    

    "matinput"

    "matInput"

    0 讨论(0)
  • 2020-11-28 04:49

    I had accidentally removed the matInput directive from the input field which caused the same error.

    eg.

    <mat-form-field>
       <input [readOnly]="readOnly" name="amount" formControlName="amount" placeholder="Amount">
    </mat-form-field>
    

    fixed code

     <mat-form-field>
       <input matInput [readOnly]="readOnly" name="amount" formControlName="amount" placeholder="Amount">
    </mat-form-field>
    
    0 讨论(0)
提交回复
热议问题