[英]How to get selected object from valueChanges but only bind object id to form using Angular reactive forms?
[英]How to detect the changes from the reactive form input using valuechanges in angular 8
我想檢測以反應形式在數量字段中所做的更改,因為我正在使用valuechanges來檢測更改。
我嘗試過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);
});
}
每當數量字段值以反應形式更改時,都應檢測到該值。
您必須從購物車表單中獲取商品數組。 然后遍歷每個項目並訂閱價值更改。
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.