I want to create a user response page for my survey app, i iterate my data through my forms using this code
for (let i = 0; i < this.listFormField.length; i++) {
let arrayForm = new FormGroup({
name: new FormControl(this.listFormField[i].name),
forms: new FormArray([])
});
(<FormArray>this.response.get("listForms")).push(arrayForm);
for (let j = 0; j < this.listFormField[i].form.length; j++) {
let formField = new FormGroup({
answer: new FormControl(""),
title: new FormControl(this.listFormField[i].form[j].title),
type: new FormControl(this.listFormField[i].form[j].type),
code: new FormControl(this.listFormField[i].form[j].code),
options: new FormControl(this.listFormField[i].form[j].options)
});
(<FormArray>this.response.controls.listForms["controls"][i].controls.forms).push(
formField
);
}
}
I think it's okay cause it displays my data exactly like i wanted, but i get unexpected behaviour, first is checkbox, like this
all of my checkbox got checked when i click one of them, and in my textarea when i try to type it's out of focus after i type 1 character. if you don't understand what i'm saying, i've recreate my issue here https://angular-ivy-uahtjx.stackblitz.io can someone help me? any thoughts would be very helpful
EDIT
here's my full html code
<form [formGroup]="response">
<div formArrayName="listForms">
<div *ngFor="let forms of formArrayControls;let i = index" formGroupName="{{i}}">
<h5>{{forms.value.name}}</h5>
<div formArrayName="forms">
<div *ngFor="let items of forms.value.forms; let j = index" formGroupName="{{j}}">
<div *ngIf="items.type == 'textarea'">
<p> {{items.title}}</p>
<textarea [id]="items.title" [name]="items.title" formControlName="answer" [placeholder]="items.title"></textarea>
</div>
<div *ngIf="items.type == 'textinput'">
<p> {{items.title}}</p>
<input type="text" [id]="items.title" [name]="items.title" formControlName="answer" [placeholder]="items.title">
</div>
<div *ngIf="items.type == 'checkbox'">
<p>{{items.title}}</p>
<div *ngFor="let option of items.options">
<label><input [id]="items.title" [name]="items.title" formControlName="answer" type="checkbox"> {{option}}</label>
</div>
</div>
</div>
</div>
</div>
</div>
<button (click)="submit()">Submit</button>
</form>