Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 999
d4rty Avatar asked Feb 11 '18 20:02

d4rty


People also ask

Can't bind to ngModel since it isn't a known property of Mat slider?

To fix Can't bind to 'ngModel' since it isn't a known property of 'input' error in Angular applications we have to import FormModule in app. module. ts file. If you are using FormBuilder class to create reactive form we have to import ReactiveFormsModule as well to avoid below error.

Can't bind to since it isn't a known property of?

What does "Can't bind to 'x' since it isn't a known property of 'y'" mean? link. This error often means that you haven't declared the directive "x" or haven't imported the NgModule to which "x" belongs. Perhaps you declared "x" in an application sub-module but forgot to export it.

What is ngModel in angular?

The ng-model directive binds the value of HTML controls (input, select, textarea) to application data.


2 Answers

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

like image 140
Mezo Istvan Avatar answered Oct 12 '22 23:10

Mezo Istvan


Please try to include FormsModule in your corresponding Module like below

@NgModule({ imports: [ BrowserModule, FormsModule ], declarations: [ AppComponent ], bootstrap: [ AppComponent ] }) 
like image 36
Derviş Kayımbaşıoğlu Avatar answered Oct 12 '22 23:10

Derviş Kayımbaşıoğlu