简体   繁体   English

如何使用角度8中的valuechanges从反应形式输入中检测变化

[英]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.

相关问题 如何从 valueChanges 获取选定的对象,但仅使用 Angular 反应形式将对象 id 绑定到表单? - How to get selected object from valueChanges but only bind object id to form using Angular reactive forms? 角反应形式valueChanges函数可仅检测和存储更改的字段 - Angular reactive form valueChanges function to detect and store only changed fields 如何取消订阅 Angular 4 反应形式的 valueChanges? - How to unsubscribe from valueChanges in Angular 4 reactive forms? Angular Reactive Form 为什么在输入侦听器之前调用 valueChanges - Angular Reactive Form why valueChanges is called before input listener 如何仅在输入模糊时触发反应形式 valueChanges 订阅并在 Angular 2 中输入键 - How to trigger reactive form valueChanges subscription only on input blur and enter key in Angular 2 Angular 反应性 Forms valueChanges - 仅 UI 更改 - Angular Reactive Forms valueChanges - UI changes only 如何在编辑模式下检测预填充的 Angular Reactive Form 中的变化 - How to detect changes in a prefilled Angular Reactive Form in Edit Mode 如何以反应形式valueChanges从递归调用中退出 - How to exit from recursive call in reactive form valueChanges Angular 5反应形式valueChanges不起作用 - Angular 5 reactive form valueChanges does not work 反应角形式随管道变化。 这是一个错误吗? - angular reactive form valuechanges with pipe. IS THIS A BUG ???
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM