0

I'm having trouble with nested FormArrays in Angular reactive forms. One of my form arrays correctly returns as a FormArray and the other returns as FormControl. In the initialMaterials() function I have two console.logs. console.log(control) returns a FormControl item and console.log(this.objectServiceJobsArray) returns a FormArray.

I need to be able to add materials to specific jobs in the array and change them in the form when necessary.

this.objectServiceForm = this.formBuilder.group({
          onHolidays: [this.objectService.onHolidays],
          objectServiceJobs: this.formBuilder.array([this.objectServiceJobs()]),
          isBillable: [this.objectService.isBillable],
          defaultPrice: [this.objectService.defaultPrice],
          pricePerHour: [this.objectService.pricePerHour],
          doneWeekly: [this.doneWeekly],
        });

objectServiceJobs(): FormGroup {
    return this.formBuilder.group({
      job: [''],
      workDetail: [''],
      competentWorkers: [[]],
      materials: this.formBuilder.array([this.objectServiceJobMaterials()])
    });
}

objectServiceJobMaterials(): FormGroup {
    return this.formBuilder.group({
      material: [null],
      quantity: [null]
    });
}

initialMaterials(job) {
    const index = (<FormArray>this.objectServiceForm.get('objectServiceJobs')).controls.findIndex(x => x.value.job.id === job.id);
    const control = (<FormArray>this.objectServiceForm.controls['objectServiceJobs']).at(index).get('materials') as FormArray;
    console.log(control);
    console.log(this.objectServiceJobsArray);

    // job.materials.forEach(mat => {
    //   this.objectServiceJobsArray[index].materials.push(this.makeMaterialFormGroup(mat));
    // });
}
6
  • 1
    I have created a stackblitz example and your code is working fine(with a small change) . Refer : stackblitz.com/edit/angular-hp7hb6 Commented Oct 31, 2019 at 9:59
  • for some reason its still returning as FormControl for me and gives the"materials.push is not a funciton" error Commented Oct 31, 2019 at 10:31
  • @CruelEngine console.log(objectServiceJobs.at(index)) also shows materials as FormControl under controls :( Commented Oct 31, 2019 at 10:53
  • 1
    I've updated the stackblitz and it is working fine as console.log(objectServiceJobs.at(index)) is returning me a FormGroup Commented Oct 31, 2019 at 11:08
  • @CruelEngine Yes, in your example everything is working as it should but for me it still returns FormControl for some reason. Commented Oct 31, 2019 at 11:13

2 Answers 2

2

I tried your code in my IDE but changed the style of extracting controls and I can see that console.log(control) returns me as FormArray.

initialMaterials(job) {
    const objectServiceJobs = this.objectServiceForm.get('objectServiceJobs') as FormArray;
    const index = objectServiceJobs.controls.findIndex(x => x.value.job.id === job.id);
    const control = objectServiceJobs.at(index).get('materials') as FormArray;
    console.log(control);
  }
Sign up to request clarification or add additional context in comments.

1 Comment

Ugh I'm still getting FormControl even though I can see in @CruelEngine s StackBlitz that it should return a FormArray :/
1

As saloo said the code works perfectly and returns a FormArray, just keep in mind that job is a form control (value isn't an object) so x.value.job.id is always undefined inside this line :

const index = objectServiceJobs.controls.findIndex(x => x.value.job.id === job.id);

2 Comments

I dont know, it seems to get the index correctly though
it's just because both variables are undefined so equality will be found always at index 0

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.