Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
how to acess formcontrols inside formarray in angular

I have the following form:

 form = this.builder.group({
  form2: this.builder.array([
      this.builder.group({
    name: new FormControl(),
    surname: new FormControl(),

  })
]),
  });

in my oninit I am doing this to set one formcontrol with a setvalue

 this.form.controls.personNameField.setValue(this.person.name);

But this does not work, probably due to the controls being inside an formarray. But how can I access my formarray controls?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can add controls with values in a FormArray like this:

get form2Array(){
  return this.form.controls.form2 as FormArrray;
}

// If form control exists at index
addPerson(index: number, person: any){
  const personFormGroup = this.form2Array.at(index) as FormGroup
  personFormGroup.setValue(person) //handle according to your code
}

// If form control does not exist at index
addPerson(index: number, person: any){
  this.form2Array.at(index).setControl(index, this.builder.group({
    name: this.builder.control({}),
    surname: this.builder.control({}),
  }))
  const personFormGroup = this.form2Array.at(index) as FormGroup
  personFormGroup.setValue(person) //handle according to your code
}
about 4 years ago · Juan Pablo Isaza Report

0

You can reach your form control with the following:

ngOnInit() {
  (this.form.get('form2') as FormArray).at(0).patchValue({ name: 'test' });
}

So we get the formarray inside the parent group, then get the first formgroup in the formarray (at index 0) and patch the value for the name formcontrol.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!