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!

7
  • What controls do assign_array contain? Commented Feb 18, 2019 at 11:45
  • only string it contains. Commented Feb 18, 2019 at 12:09
  • you need to set formControlName in ion input for reactive form to work Commented Feb 18, 2019 at 12:16
  • what object name to set on formControlName ? Commented Feb 18, 2019 at 12:19
  • depends on the controls in the array this.clientForm = this._fb.group({ assign_array: this._fb.array([]) }); Commented Feb 18, 2019 at 12:28

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

OP is using reactive form not ngModel
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>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.