'Ionic 4 - Ionic Input Two-Way Binding
In Ionic 4 how do you do two-way binding. In Ionic 3 I would do the following:
<ion-item color="light"> <ion-input type="string" placeholder="Username" [(ngModel)]="username"></ion-input> </ion-item>
However in Ionic 4 I get the following error:
Can't bind to 'ngModel' since it isn't a known property of 'ion-input'.
1. If 'ion-input' is an Angular component and it has 'ngModel' input, then verify that it is part of this module.
2. If 'ion-input' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("d>
<ion-item color="light">
<ion-input type="string" placeholder="Username" [ERROR ->][(ngModel)]="username"></ion-input>
</ion-item>
<ion-item color="light">
"): ng:///AppModule/LoginPage.html@12:62
How do I get this working in Ionic 4?
Solution 1:[1]
Please keep in mind to add the FormsModule to your module or create a SharedModule to import and export FormsModule
@NgModule({
imports: [
CommonModule,
FormsModule,
IonicModule,
RouterModule.forChild([
{
path: '',
component: HomePage
}
])
],
declarations: [HomePage]
})
export class HomePageModule { }
Solution 2:[2]
Ionic 3/4 with angular 6
In order to be able to use two-way data binding for form inputs you need to import the FormsModule package in your Angular module. For more info see the Angular official tutorial here
For Example
import { FormsModule } from '@angular/forms';
[...]
@NgModule({
imports: [
[...]
FormsModule
],
[...]
})
Solution 3:[3]
You just need to import FormsModule in app.module.ts. As i have already gave the answer in detail. Visit to the link for detail https://stackoverflow.com/a/55684045/7983887
Solution 4:[4]
Add FormsModule in the your Angular module
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Deepika Wadhera |
Solution 2 | Manvender Singh Rathore |
Solution 3 | Tahseen Quraishi |
Solution 4 | Akhil Sivanandan |