I have question how can i iterate through formArray and use inside data from other array. I added example below:
@Component({
selector: 'app-reactive-form-test',
styleUrls: ['./reactive-form-test.component.scss'],
template: `
<form [formGroup]="questionForm">
<ng-container formArrayName="questions">
<ng-container *ngFor="let question of questionControls.controls; let i = index">
<input type="text" [formGroupName]="i">
{{?????? how to display prop1}}
{{?????? how to display prop2}}
</ng-container>
</ng-container>
</form>
`
})
export class ReactiveFormTestComponent implements OnInit {
questionForm: FormGroup;
questions: ModelClass[];
get questionControls() {
return this.questionForm.get('questions') as FormArray;
}
constructor(private fb: FormBuilder) { }
ngOnInit(): void {
this.questionForm = this.fb.group({
questions: this.fb.array([])
});
this.questions = [];
this.questions.push(new ModelClass(1, 1, 1, 1));
this.questions.push(new ModelClass(2, 3, 1, 2));
this.questions.push(new ModelClass(3, 4, 1, 3));
this.questions.forEach(value => {
const control = this.questionForm.get('questions') as FormArray;
control.push(this.fb.group({
id: [value.id],
prop1: [value.prop1]
}));
});
}
}
export class ModelClass {
id,
prop1,
prop2,
prop3
}
And my question is how can i dispaly inside formArray loop data rom model (questions) - prop2, prop3 ??
CodePudding user response:
<ng-container *ngFor="let question of questionControls.controls; let i= index">
{{questions[i].prop1}}
{{questions[i].prop2}}
{{questions[i].prop3}}
</ng-container>