mat-form-field must contain a MatFormFieldControl

前端 未结 30 1541
萌比男神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:49

    In my case, one of my closing parenthesis for "onChanges()" were missed on the input element and thus the input element was apparently not being rendered at all:

    <input mat-menu-item 
          matInput type="text" 
          [formControl]="myFormControl"
          (ngModelChange)="onChanged()>
    
    0 讨论(0)
  • 2020-11-28 04:50

    Unfortunately I can't just comment on some already good answers as I don't have the SO points yet, however, there are 3 key modules that you'll need to make sure you are importing into your component's parent module, aside from what you have to import into your component directly. I wanted to briefly share them and highlight what they do.

    1. MatInputModule
    2. MatFormFieldModule
    3. ReactiveFormsModule

    The first two are for Angular Material. A lot of people new to Angular Material will instinctively come across one of these and not realize that building a form requires both.

    So what's the difference between them?

    MatFormFieldModule encompasses all the different types of form fields that Angular Material has available. This is more of a high level module for form fields in general, whereas the MatInputModule is specifically for 'input' field types, as opposed to select boxes, radio buttons, etc.

    The third item on the above list is Angular's Reactive Forms Module. The Reactive Forms Module is responsible for a ton of under-the-hood Angular love. If you are going to work with Angular, I highly recommend that you spend some time reading Angular Docs. They have all of your answers. Building applications rarely DOESN'T involve forms, and more often than not, your application will involve reactive forms. For that reason, please take the time to read these two pages.

    Angular Docs: Reactive Forms

    Angular Docs: Reactive Forms Module

    The first doc 'Reactive Forms' will be your most powerful weapon as you get started, and the second doc will be your most power weapon as you get on to more advanced applications of Angular Reactive Forms.

    Remember, these need to be imported directly into your component's module, not the component you are using them in. If I remember correctly, in Angular 2, we imported the entire set of Angular Material modules in our main app module, and then imported what we needed in each of our components directly. The current method is much more efficient IMO because we are guaranteed to import the entire set of Angular Material modules if we only use a few of them.

    I hope this provides a bit more insight into building forms with Angular Material.

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

    You need to import the MatInputModule into your app.module.ts file

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

    import { NgModule } from '@angular/core';
    import { CommonModule } from '@angular/common';
    import { FormsModule } from '@angular/forms';
    import { IonicModule } from '@ionic/angular';
    import { CustomerPage } from './components/customer/customer';
    import { CustomerDetailsPage } from "./components/customer-details/customer-details";
    import { CustomerManagementPageRoutingModule } from './customer-management-routing.module';
    import { MatAutocompleteModule } from '@angular/material/autocomplete'
    import { ReactiveFormsModule } from '@angular/forms';
    import { MatFormFieldModule } from '@angular/material/form-field';
    import { MatInputModule} from '@angular/material';
    
    @NgModule({
      imports: [
        CommonModule,
        FormsModule,
        IonicModule,
        CustomerManagementPageRoutingModule,
        MatAutocompleteModule,
        MatInputModule,
        ReactiveFormsModule,
        MatFormFieldModule
      ],
    
    0 讨论(0)
  • 2020-11-28 04:50

    Angular 9+ ...Material 9+

    I have noticed 3 mistakes can can give rise to the same error:

    1. Ensure you have imported MatFormFieldModule and MatInputModule in the app.module or the module.ts where your components are being imported(in case of nested modules)
    2. When you wrap material buttons or checkbox in mat-form-field. See the list of material components that can be wrapped with mat-form-field mat-form-field
    3. When you fail to include matInput in your tag. i.e <input matInput type="number" step="0.01" formControlName="price" />
    0 讨论(0)
  • 2020-11-28 04:53

    This can also happen if you have a proper input within a mat-form-field, but it has a ngIf on it. E.g.:

    <mat-form-field>
        <mat-chip-list *ngIf="!_dataLoading">
            <!-- other content here -->
        </mat-chip-list>
    </mat-form-field>
    

    In my case, mat-chip-list is supposed to "appear" only after its data is loaded. However, the validation is performed and mat-form-field complains with

    mat-form-field must contain a MatFormFieldControl

    To fix it, the control must be there, so I have used [hidden]:

    <mat-form-field>
        <mat-chip-list [hidden]="_dataLoading">
            <!-- other content here -->
        </mat-chip-list>
    </mat-form-field>
    

    An alternative solution is proposed by Mosta: move *ngIf for mat-form-field:

    <mat-form-field *ngIf="!_dataLoading">
        <mat-chip-list >
            <!-- other content here -->
        </mat-chip-list>
    </mat-form-field>
    
    0 讨论(0)
  • 2020-11-28 04:53

    If anyone got stuck with this error after attempting to nest a <mat-checkbox>, be of good cheer! It doesn't work inside a <mat-form-field> tag.

    0 讨论(0)
提交回复
热议问题