Search code examples
angularangular-materialangular5angular-material-5

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


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


Solution

  • 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