[英]How to detect the changes from the reactive form input using valuechanges in angular 8
I want to detect the changes made in the quantity field made in the reactive form for that I am using valuechanges in order to detect the changes. 我想检测以反应形式在数量字段中所做的更改,因为我正在使用valuechanges来检测更改。
I tried valuechanges but it showing error as Cannot read property 'valueChanges' of undefined 我尝试过valuechanges,但显示错误,因为无法读取未定义的属性'valueChanges'
ngOnInit() {
this.cartItems = this.productService.getCalculateProducts();
this.cartItems = this.productService._cartItem;
this.initForm();
this.cartFormChanges();
console.log(this.cartForm.controls['items']);
}
initForm() {
this.cartForm = this.fb.group({
items: this.fb.array([])
})
this.cartItems.forEach(item => {
(this.cartForm.get('items') as FormArray).push(this.createFormItem(item));
});
}
createFormItem(product: Product): FormGroup {
return this.fb.group({
id: product.id,
qty: [product.productQty, [Validators.max(product.productQty), Validators.min(1)]],
price: product.productPrice
});
}
get items() {
return this.cartForm.controls.items as FormArray;
}
cartFormChanges() {
this.cartForm.controls["qty"].valueChanges.subscribe((changes) => {
console.log('Object', changes);
console.log('qty', this.cartForm.value.qty);
});
}
The value should detected whenever the quantity field value is changed in the reactive form. 每当数量字段值以反应形式更改时,都应检测到该值。
You have to get the items array from cart form. 您必须从购物车表单中获取商品数组。 Then iterate over each item and subscribe for value change.
然后遍历每个项目并订阅价值更改。
const control = <FormArray>this.cartForm.controls['items'];
for (i = 0; i < control.length; i++) {
control.controls[i].get("qty").valueChanges.subscribe(x => {
console.log(x)
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.