I initialise a form with any empty array in definitionProperties
how can I update FormGroup
? I need to do so without triggering a change event otherwise it causes an infinite loop in the valueChanges
observable.
this.gridProcessorForm = this.fb.group({
propertiesSide: new FormControl(),
objectNamesSide: new FormControl(),
definitionProperties: this.fb.array([])
});
This causes the infinite loop:
this.gridProcessorForm.valueChanges.subscribe(val => {
let propertyGroups: FormGroup[] = [];
for (let property of this.objectDefinition.properties) {
let group = this.createPropertyFormGroup(property);
(this.gridProcessorForm.get('definitionProperties') as FormArray).push(group); // triggers change event so infinite loop
}
});
When using setValue with { emitEvent: false }
to overwrite the FormArray it gets an error.
this.gridProcessorForm.valueChanges.subscribe(val => {
if (this.gridSideSelection = 'top') {
let propertyGroups: FormGroup[] = [];
for (let property of this.objectDefinition.properties) {
let group = this.createPropertyFormGroup(property);
propertyGroups.push(group);
}
(this.gridProcessorForm.get('definitionProperties') as FormArray).setValue(propertyGroups, { emitEvent: false });
}
});
I get this error:
There are no form controls registered with this array yet.
It seems it requires the form builder to initialise with a group in the FormArray? But I want to initialise the form with everything empty.
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.
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.
Whereas FormGroup represents an entire form or a fixed subset of a form's fields, FormArray usually represents a collection of form controls that can grow or shrink.
There's an issue in angular's repo. Please refer this comment https://github.com/angular/angular/issues/23336#issuecomment-543209703
Basically, you need to do it like this:
const addedControl = new FormControl();
this.controlArray.controls.push(addedControl);
this.controlArray['_registerControl'](addedControl);
This was fixed in Angular v12 https://github.com/angular/angular/issues/20439#issuecomment-779988084
I hope this will help as this is my running code.
let containersArray = <FormArray>this.formName.controls['containers'];
containersArray.push(this.addContainerInitialize(container));
addContainerInitialize(value) {
return this.fb.group({
containerNumber: [value.containerNumber],
conType: [value.conType, Validators.required],
quantity: [value.noOfContainers, Validators.required],
weight: [value.grossWeight, Validators.required]
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With