Can't bind to 'ngModel' since it isn't a known property of 'mat-slide-toggle'
Asked Answered
C

3

24

I am trying to get the current value of a mat-slide-toggle but unfortunatly I will get an error:

Error: Template parse errors:
Can't bind to 'ngModel' since it isn't a known property of 'mat-slide-toggle'.

I am using the toggle like this in the html part of my component:

 <mat-slide-toggle color="primary" [(ngModel)]="showInnerView">
      Default Slide Toggle
 </mat-slide-toggle>

Corresponding property in my component:

showInnerView: boolean = false;

What I am doing wrong?


Documentation of mat-slide-toggle

Official example of mat-slide-toggle together with [(ngModel)]


Used Versions: Angular: 5.2.4, Angular Material: 5.2.0

Crook answered 11/2, 2018 at 20:3 Comment(0)
I
34

ngModel lives in FormsModule of @angular/forms, so import that to your AppModule (or whichever module you are trying to use it in).

Also see this question: Angular 2 two way binding using ngModel is not working

Immunize answered 11/2, 2018 at 20:10 Comment(0)
M
15

Please try to include FormsModule in your corresponding Module like below

@NgModule({
imports: [ BrowserModule, FormsModule ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
Mantic answered 11/2, 2018 at 20:18 Comment(1)
I had import @angular/forms included, but only used the ReactiveFormsModule. Including FormsModule helped!Grayback
P
-1

i had the same problem, after running

ng serve

it was fixed

Plaice answered 30/7, 2021 at 8:22 Comment(3)
This is not an answer as it does not provide any solution or explanation, please have a look at the tour from StackOverflow on how to answer questions: stackoverflow.com/helpLuting
@Ferie, actually it helped me xD I had this error even tho I had FormsModule imported in my component's module. Restarting the application removed this error. Strange, but it works.Fidgety
@Fidgety I hope you recognise that ng serve is just running locally the application: it does not solve the issue by itself. You must have changed something in the app to let the build works.Luting

© 2022 - 2024 — McMap. All rights reserved.