我正在使用 Angular Reactive Forms 遍历一个值数组,我希望在 Form Array 之后有一个总字段,用于更新 Form Array 控件值的更改。
样本数据:
primaryData = {
products: [
{
name: 'test-product',
unmoved: 21,
moved: 18
},
{
name: 'another-product',
unmoved: 18,
moved: 42
}
]
}
我正在创建反应式表单控件和数组,如下所示:
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();
}
根据数组控件中的更改更新我的控件unmovedTotal
和控件的最佳方法是什么。movedTotal
这是一个StackBlitz展示我的结构。