mat-form-field must contain a MatFormFieldControl

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

    Unfortunately content projection into mat-form-field is not supported yet. Please track the following github issue to get the latest news about it.

    By now the only solution for you is either place your content directly into mat-form-field component or implement a MatFormFieldControl class thus creating a custom form field component.

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

    use providers in component.ts file

    @Component({
     selector: 'your-selector',
     templateUrl: 'template.html',
     styleUrls: ['style.css'],
     providers: [
     { provide: MatFormFieldControl, useExisting: FormFieldCustomControlExample }   
    ]
    })
    
    0 讨论(0)
  • 2020-11-28 04:55

    I had same issue

    issue is simple

    only you must import two modules to your project

    MatFormFieldModule MatInputModule

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

    I had this issue too, and I have <mat-select> element in my template, when I import the MatSelectModule into Module, it works, it doesn't make science, but stil hope it can help you.

    import { MatSelectModule } from '@angular/material/select';
    
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        MatSidenavModule,
        MatButtonModule,
        MatIconModule,
        MatToolbarModule,
        MatFormFieldModule,
        MatProgressSpinnerModule,
        MatInputModule,
        MatCardModule,
        MatSelectModule
    ],
    
    <div class="example-container">
        <mat-form-field>
            <input matInput placeholder="Input">
        </mat-form-field>
    
        <mat-form-field>
            <textarea matInput placeholder="Textarea"></textarea>
        </mat-form-field>
    
        <mat-form-field>
            <mat-select placeholder="Select">
                <mat-option value="option">Option</mat-option>
            </mat-select>
        </mat-form-field>
    </div>
    
    0 讨论(0)
  • 2020-11-28 04:57

    You have missed matInput directive in your input tag.

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

    I had the module imported and directive set and after executing 'ng add @angular/material' again it started working. Maybe it would be enough just to restart app with 'ng serve'

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