Performance: FormArray for dynamic fields
This affects the rendering speed and responsiveness when managing multiple dynamic form controls in Angular.
this.form = new FormGroup({ fields: new FormArray([]) }); addField() { (this.form.get('fields') as FormArray).push(new FormControl('')); } removeField(index: number) { (this.form.get('fields') as FormArray).removeAt(index); }
this.form = new FormGroup({ fields: new FormGroup({ field1: new FormControl(''), field2: new FormControl(''), // ...manually adding each field }) }); // Adding/removing fields requires recreating the whole FormGroup
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual FormGroup for each dynamic field | High (many nodes added/removed individually) | Multiple reflows per field change | High paint cost due to full form re-render | [X] Bad |
| Using FormArray for dynamic fields | Low (only affected controls updated) | Single reflow per add/remove | Low paint cost with targeted updates | [OK] Good |
FormArray in Angular forms?FormArray named emails?push().push() correctly. Others are invalid methods for FormArray.FormArray after execution?const tags = new FormArray([]);
tags.push(new FormControl('angular'));
tags.push(new FormControl('typescript'));
tags.removeAt(0);phones:phones.remove(1);
removeAt(index) to remove controls by position.remove(1) is invalid because remove is not a FormArray method.FormArray and adds a new empty email control when a button is clicked?new FormArray([]).push(new FormControl('')) to add a new empty control dynamically.Pass the test and this pattern moves unknown