Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 410
Ka Tech Avatar asked Sep 12 '18 22:09

Ka Tech


People also ask

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

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.

How do you bind data in ionic?

Ionic Two-way data binding can be achieved using a ngModel directive in Angular. The 2-way data binding using (ngModel), is basically the combination of both the square brackets of property binding and parentheses of the event binding.


1 Answers

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 { }
like image 169
Deepika Wadhera Avatar answered Sep 28 '22 08:09

Deepika Wadhera