0

I am using reactive forms with a form builder to populate my form.

I defined my form like this:

public myForm = this.formBuilder.group({
  something: [''],
  people: this.formBuilder.array([this.newPerson()]),
});

The newPerson() function looks like this:

public newPerson(): FormGroup {
  return this.formBuilder.group({
    firstName: [''],
    lastName: [''],
  },);
}

In OnInit() I want to populate this form with some data and here is where I have issues. If I do:

this.getPeople().patchValue([
  { firstName: 'Bla', lastName: 'Bla' },
  { firstName: 'Test', lastName: 'Test' }, 
]);

I only see the Bla Bla in my form but not Test Test. Why is that so?

1
  • 1
    this.formBuilder.array([this.newPerson()]) => this creates a formArray with single formGroup. You'd have to call this.newPerson() twice to create 2 formgroups, and then patchValue Commented Nov 26, 2021 at 8:52

1 Answer 1

1

this.formBuilder.array([this.newPerson()]) => this creates a formArray with single formGroup. You'd have to call this.newPerson() twice to create 2 formgroups, and then patchValue

public myForm = this.formBuilder.group({
  something: [''],
  people: this.formBuilder.array([this.newPerson(), this.newPerson()]),
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.