1

I have a FormArray with 3 FormGroups and each form group has 2 controls. Im not sure how to access the controls within the form groups to set the form control name.

setting up the form

this.form = this.formBuilder.group({
---a couple controls---
properties: this.formBuilder.array({
  first: this.formBuilder.group({ 
   label: new UntypedFormControl(),
   value: new UntypedFormControl(),
  }),
  second: this.formBuilder.group({ 
   label: new UntypedFormControl(),
   value: new UntypedFormControl(),
  }),
  third: this.formBuilder.group({ 
   label: new UntypedFormControl(),
   value: new UntypedFormControl(),
  }),
})
})

getter for properties form array

get properties() {
  return this.form.controls.properties as FormArray;
}

im able to add and delete a row of properties.

html for the form. Am not sure how to access the label and value controls in each form group from the form array.

<div formArrayName="properties">
  <ng-container *ngFor="let propertiesForm of properties.controls; let i = index">
  <div [formGroupName]="i">
---then I have 3 sets of mat form fields each with and label and value---
  </div>
  </ng-container>
</div>
1
  • formBuilder.array accept parameter which is an array value, but you are providing an object. I don't think this is working. If you want to adapt your object with first, second properties, you should consider to use the FormGroup. Commented Apr 6, 2024 at 0:56

1 Answer 1

2

this.formBuilder.array() method only accepts a parameter which is an array. You can't provide an object to it.

Approach 1: With this.formBuilder.array([])

  1. Modify your properties structure to an array.
  2. You can work with a loop to add the number of FormGroup that you need in the properties FormArray. And you should consider to implement a function to generate the single property FormGroup. This aims to reduce the code redundancy.
ngOnInit() {
  // Form with Form Array
  this.form = this.formBuilder.group({
    //---a couple controls---
    properties: this.formBuilder.array([]),
  });

  for (let i = 0; i < 3; i++)
    this.properties.push(this.createPropertyFormGroup());
}

createPropertyFormGroup() {
  return this.formBuilder.group({
    label: new UntypedFormControl(),
    value: new UntypedFormControl(),
  });
}
<form [formGroup]="form">
  <div formArrayName="properties">
    <ng-container
      *ngFor="let propertiesForm of properties.controls; let i = index"
    >
      <div [formGroupName]="i">
        <label>Label: </label>
        <input formControlName="label" />
        <br />

        <label>Value: </label>
        <input formControlName="value" />
      </div>
      <hr />
    </ng-container>
  </div>
</form>

Approach 2: With this.formBuilder.group({})

  1. In HTML, you need the KeyValuePipe to iterate the properties FormGroup object and generate each FormGroup.
this.form = this.formBuilder.group({
  //---a couple controls---
  properties: this.formBuilder.group({
    first: this.createPropertyFormGroup(),
    second: this.createPropertyFormGroup(),
    third: this.createPropertyFormGroup(),
  }),
});

get propertiesFormGroup() {
  return this.form.controls['properties'] as FormGroup;
}
<form [formGroup]="form">
  <div formGroupName="properties">
    <ng-container
      *ngFor="let propertiesForm of propertiesFormGroup.controls | keyvalue;"
    >
      <div [formGroupName]="propertiesForm.key">
        <label>Label: </label>
        <input formControlName="label" />
        <br />

        <label>Value: </label>
        <input formControlName="value" />
      </div>
      <hr />
    </ng-container>
  </div>
</form>

Demo @ StackBlitz

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.