Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Model Driven Form Nested FormArray Control in a FormGroup control

I have a form group like so:

 this.form = fb.group({
  'teacher': [''],
  'schools': fb.array([
      fb.group({
        'school_name': [''],
        'school_description': [''],
        'events': fb.array([
          fb.group({
            'event_name': ['']
          })
        ])
      })
  ])
});
const control = (<FormArray>this.form.controls['schools']).controls[0]['events'];

How do I get the nested array control 'events'?

const control = (<FormArray>this.form.controls['schools']).controls[0]['events'];
like image 292
edumas000 Avatar asked Jan 09 '17 13:01

edumas000


People also ask

Can FormArray contains FormGroup?

The FormArray is a way to Manage collection of Form controls in Angular. The controls can be FormGroup, FormControl, or another FormArray. Because it is implemented as Array, it makes it easier dynamically add controls.

How do you use formControlName and deal with nested FormGroup?

With a parent FormGroup, the name input needs the syntax formControlName=name in order to be associated with the correct FormControl in the class. This syntax tells Angular to look for the parent FormGroup, in this case heroForm, and then inside that group to look for a FormControl called name.

How do I use FormGroup in FormArray?

We do not have a name to the FormGroup . The Index of the element is automatically assigned as the name for the element. Hence we use the [formGroupName]="i" where i is the index of the FormArray to bind the FormGroup to the div element. Finally, we add the controls using the formControlName directive.


2 Answers

I recommend having a look at the Angular 2 examples for nested form array and nested form group for a look at how to build complex forms. With FormGroup, you can use .get(name: string) to retrieve a control. This example gets the schools FormArray control:

this.form.get('schools')

For FormArray, you can retrieve a control from the array using .at(index: number). This example gets the first FormGroup control in the array:

this.schools.at(0)

To put it all together, there are nicer (read: more reusable, more readable) ways to express this but this chain will get you the first event from your first school:

this.form.get('schools').at(0).get('events')

Here's a working plnkr example.

like image 116
stealththeninja Avatar answered Oct 12 '22 12:10

stealththeninja


You should use this code:

      let listForm: any;
      var subFormGroup = new FormGroup({});
        subFormGroup.addControl("Your Controll Name", new FormControl('',res["Required"] ? Validators.required : null))

      listForm = this.builder.array([
        subFormGroup
      ]);
      this.form.addControl("Your new ArrayControll Name", listForm);

When you have an Array of Object,you can use that:

 let listForm: any;
 var controlItems =[Your Object Array];
 var subFormGroup = new FormGroup({});
 Object.keys(controlItems).forEach(function(key){
    subFormGroup.addControl(controlItems[key], new FormControl('',res["Required"] ? Validators.required : null))
  })
 listForm = this.builder.array([
     subFormGroup
 ]);
 this.form.addControl("Your new ArrayControll Name", listForm);
like image 23
Saeed Avatar answered Oct 12 '22 11:10

Saeed