Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2. How to use array of objects for controls in Reactive Forms

Tags:

forms

angular

I need to dynamic create textarea for forms. I have the following model:

this.fields = {       isRequired: true,       type: {         options: [           {             label: 'Option 1',             value: '1'           },           {             label: 'Option 2',             value: '2'           }         ]       }     }; 

And form:

this.userForm = this.fb.group({       isRequired: [this.fields.isRequired, Validators.required],       //... here a lot of other controls       type: this.fb.group({          options: this.fb.array(this.fields.type.options),       }) }); 

Part of template:

<div formGroupName="type">        <div formArrayName="options">          <div *ngFor="let option of userForm.controls.type.controls.options.controls; let i=index">             <textarea [formControlName]="i"></textarea>          </div>       </div> </div> 

So, as you can see I have an array of objects and I want to use label property to show it in a textarea. Now I see [object Object]. If I change options to a simple string array, like: ['Option 1', 'Option 2'], then all works fine. But I need to use objects. So, instead of:

<textarea [formControlName]="i"></textarea> 

I have tried:

<textarea [formControlName]="option[i].label"></textarea> 

But, it doesn't work.
How can I use an array of objects?

This is Plunkr

like image 278
user348173 Avatar asked Mar 23 '17 06:03

user348173


People also ask

Can a form control be an array?

What's a FormArray. A FormArray is responsible for managing a collection of AbstractControl , which can be a FormGroup , a FormControl , or another FormArray . Just like a FormGroup , which groups AbstractControl objects in an object, a FormArray does the same but in an array.

How do you create a form array?

First, we need to import the FormArray from the Angular Forms Module. Build a formGroup orderForm using the FormBuilder. We define items as FormArray. We need to capture two fields under each item, the name of the item & description and price.

Can I use FormArray without FormGroup?

You just need to name the formControls inside your formArray .

How is FormArray used in Reactive form?

Binding FormArray to Template We use the formArrayName directive to bind the skills form array to the div element. Now the div and anything inside the div element is bound to the skills form array. Inside the div use ngFor to loop through each element of skills FormArray.


1 Answers

You need to add a FormGroup, which contains your label and value. This also means that the object created by the form, is of the same build as your fields object.

ngOnInit() {   // build form   this.userForm = this.fb.group({     type: this.fb.group({       options: this.fb.array([]) // create empty form array        })   });    // patch the values from your object   this.patch(); } 

After that we patch the value with the method called in your OnInit:

patch() {   const control = <FormArray>this.userForm.get('type.options');   this.fields.type.options.forEach(x => {     control.push(this.patchValues(x.label, x.value))   }); }  // assign the values patchValues(label, value) {   return this.fb.group({     label: [label],     value: [value]   })     } 

Finally, here is a

Demo

like image 57
AT82 Avatar answered Oct 11 '22 02:10

AT82