0

I want to bind my dynamic input fields with ionic html template.

home.html

    <form [formGroup]="clientForm">
     <ion-item *ngFor="let obj of clientForm.controls.assign_array.controls;let z=index">
            <ion-input placeholder="Type dat" type="text"></ion-input>
          </ion-item>
    </form>

home.ts

constructor(){
 this.clientForm = this._fb.group({
   assign_array: this._fb.array([])
 });
}

On save click:

btnClick(){
    console.log("clintform--- " + JSON.stringify(this.clientForm.value));
}

Output: { "assign_array":[ "", "", "" ] }

I can see multiple input fields in my app, but when i type something to each field my log doesn't show the value of assign_array fields

Where i am making mistake?

Thank you in advance!

Krunal Lathia
  • 75
  • 2
  • 14

2 Answers2

0

You have to just use [(ngModel)] in your html like this.

<ion-input placeholder="Type dat" type="text" [(ngModel)]="inputFieldValue"></ion-input>

and in .ts file

public inputFieldValue;
console.log("--------inputFieldValue-------",inputFieldValue)

Hope this can help you out.

0

You need to mark a formControlName for your inputs, since you want string values, you can just use the index as formcontrolname, which you get from your iteration:

<form [formGroup]="clientForm">
  <ion-item *ngFor="let obj of clientForm.get('assign_array').controls; let z=index">
    <ion-input [formControlName]="z" placeholder="Type dat" type="text"></ion-input>
  </ion-item>
</form>
AT82
  • 71,416
  • 24
  • 140
  • 167