0

I am using ionic 7 with angular to build social app and I want to use two way binding to get the data from the input field but ngModel is not recognise Here is the html content:

<div class="post-input">
<ion-item>
  <ion-input
    placeholder="Write your today's post ..."
    [(ngModel)]="postText"
    [ngModelOptions]="{standalone: true}"
  ></ion-input>
</ion-item>
<ion-button class="post-btn" color="medium" (click)="post()">
  <ion-icon name="pencil-outline"></ion-icon>
</ion-button>

and the ts file:

import { Component } from '@angular/core';
import { IonicModule } from '@ionic/angular';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
  standalone: true,
  imports: [IonicModule],
})
export class HomePage {

  postText!: string
  constructor() {}

  post() {

  }
}

Now I am getting the error Can't bind to 'ngModelOptions' since it isn't a known property of 'ion-input' and the old solutions is not working.

JSON Derulo
  • 9,780
  • 7
  • 39
  • 56

2 Answers2

3

The inputs for template-driven forms, such as ngModel and ngModelOptions, are only available when importing FormsModule in your standalone feature component (or feature module):

import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { IonicModule } from '@ionic/angular';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
  standalone: true,
  imports: [IonicModule, FormsModule],
})

It's not enough to import FormsModule in your standalone root component (or root module).

For more information, see the Angular guide on building a template-driven form.

JSON Derulo
  • 9,780
  • 7
  • 39
  • 56
1

Make sure you have imported the FormsModule in your app.module.ts file.

import { FormsModule } from '@angular/forms';
@NgModule({
  imports: [
    FormsModule
  ]
})

If you are still having an issues, make sure you have imported "Ionicmodule" and "Commonmodule" in your app.module.ts file.

import { IonicModule } from '@ionic/angular';
import { CommonModule } from '@angular/common';

@NgModule({
   imports: [
    IonicModule,
    CommonModule,
    FormsModule       
   ],  
})
Prajapati Vikas
  • 294
  • 1
  • 10