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
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.
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.
The ng-model directive binds the value of HTML controls (input, select, textarea) to application data.
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
Please try to include FormsModule in your corresponding Module like below
@NgModule({ imports: [ BrowserModule, FormsModule ], declarations: [ AppComponent ], bootstrap: [ AppComponent ] })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With