I am using Angular Reactive Forms to iterate through an array of values I would like to have a total field after the Form Array that updates on changes of the Form Array control values.
Sample data:
primaryData = {
products: [
{
name: 'test-product',
unmoved: 21,
moved: 18
},
{
name: 'another-product',
unmoved: 18,
moved: 42
}
]
}
I am creating the Reactive Form Controls and Array as follows:
setPrimaryQuantities() {
const control = <FormArray>this.primaryForm.controls.quantities;
this.primaryData.products.forEach(product =>
control.push(this.fb.group({
name: [product.name, Validators.required],
unmoved: [product.unmoved, Validators.required],
moved: [product.moved, Validators.required]
}))
)
}
ngOnInit() {
this.primaryForm = this.fb.group({
quantities: this.fb.array([]),
unmovedTotal: '',
movedTotal: ''
})
this.setPrimaryQuantities();
}
What is the best way to have my unmovedTotal
and movedTotal
Controls update based on the changes in the Array controls. Here's a StackBlitz demonstrating my structure.