25

If I have a mat-slide-toggle inside a mat-form-field I get the error:

Error: mat-form-field must contain a MatFormFieldControl

I want my mat-slide-toggle inside my mat-form-field so i can get the styling. Any ideas why i get this error? Is a mat-slide-toggle not a mat-form-field???

Here is an example of what I currently have:

<mat-form-field fxFlexFill *ngSwitchCase="'mat-slide-toggle'">
    <mat-slide-toggle>{{el.text}}</mat-slide-toggle>
</mat-form-field>
4

5 Answers 5

27

I present two slightly different hacks with no underline based upon multiple answers

Label above with extended clickable white space, uses css

enter image description here

<mat-form-field floatLabel="always" appearance="none">
  <mat-label>Override Speed</mat-label>
  <mat-slide-toggle [(ngModel)]="Override"><span class="text-invisible">Override Speed</span></mat-slide-toggle>
  <textarea matInput hidden></textarea>
</mat-form-field>
.text-invisible {
  opacity: 0;
}

Label to the right, no css

enter image description here

<mat-form-field floatLabel="always" appearance="none">
  <mat-slide-toggle [(ngModel)]="Override">Override Speed</mat-slide-toggle>
  <textarea matInput hidden></textarea>
</mat-form-field>
6
  • 1
    That is exactly what i was looking for. Thank you! Commented Aug 10, 2020 at 5:34
  • 2
    I got: Type '"none"' is not assignable to type 'MatFormFieldAppearance'.
    – joseluisbz
    Commented Nov 16, 2021 at 17:24
  • I have not seen this module as of yet. I would verify the app.module file contains the right imports. Possibly share your code or ask a new question and share he link to it here.
    – pcnate
    Commented Nov 17, 2021 at 11:45
  • 1
    Minor notes from a few years later - only making the text invisible still leaves the label target accessible, so you've got a pointer cursor active in a whitespace area. I replaced the text-opacity with display: none. As others mentioned, "none" is not an assignable appearance in material (at least not in 2022). I found that removing the appearance attribute altogether fixed that.
    – dst3p
    Commented Aug 25, 2022 at 13:30
  • 1
    Up from Angular v15.x.x this is not longer possible as appearance="none" is not a valid value
    – A.Casanova
    Commented Feb 28, 2023 at 10:31
8

You can, however, put a hidden text field inside the mat-form-field to satisfy the requirement and, if necessary, bind all the values together if what you want is to get the same look as the other fields.

<mat-form-field floatLabel="always">
        <mat-label>Label</mat-label>
        <mat-slide-toggle>Raw</mat-slide-toggle>
        <textarea matInput hidden></textarea>
    </mat-form-field>
4
  • How do you get rid of the underline when doing this?
    – Robert
    Commented Nov 12, 2019 at 17:26
  • @Robert add appearance="none" to the <mat-form-field>
    – pcnate
    Commented Dec 11, 2019 at 16:23
  • 1
    @Robert I used .mat-form-field-underline { display: none; }
    – Wungsow
    Commented Mar 4, 2020 at 2:57
  • @Wungsow solution works onyl because appearance has not none option. May I suggest to use class name for matform-field and add ::ng-deep to selector with spesific class name to not broke other places. Commented Dec 9, 2021 at 14:45
2

Try adding a hidden input field

<mat-form-field>
  <mat-slide-toggle class="example-margin" [color]="color" [checked]="checked"
    [disabled]="disabled">
    Cumulative
  </mat-slide-toggle>
  <input matInput #value hidden />
</mat-form-field>
2
  • how do you get rid of the underline when doing this?
    – Robert
    Commented Nov 12, 2019 at 17:25
  • @Robert either via css, or by setting the [appearance] input of the form field to a invalid value, like <mat-form-field class="w100p" [appearance]="'nope'">. This way, the css styles of the underline will not be applied (as shown in the source)
    – illeb
    Commented Nov 14, 2019 at 10:13
1

According to official documentation you can't do this (link).

The following Angular Material components are designed to work inside a <mat-form-field>:

  • <input matNativeControl> & <textarea matNativeControl>
  • <select matNativeControl>
  • <mat-select>
  • <mat-chip-list>
2
-3

Unfortunately Mat-Form-Fields do not work/function when a mat-slide toggle is inside of them. I had to find this out the hard way myself.

Try this instead:

<div fxFlexFill *ngSwitchCase="'mat-slide-toggle'">
    <mat-slide-toggle>{{el.text}}</mat-slide-toggle>
</div>
1
  • this isnt remotely true, you can add a hidden input field <input matInput #value hidden />
    – Nathan
    Commented Oct 30, 2019 at 14:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.