简体   繁体   English

如何订阅 formGroup 更改并从其他两个属性中计算一个?

[英]How can I subscribe to formGroup changes and calculate one of properties from two other?

I have function creates formGroup and I need to calculate one of field from two others: sum = price * count.我有函数创建 formGroup,我需要从其他两个字段中计算一个:sum = price * count。 How can I do that?我怎样才能做到这一点?

public createService = (): FormGroup => {
    const group = this.fb.group({
      name: [''],
      measure: [''],
      count: [''],
      price: [''],
      sum: ['']
    });

    group.valueChanges.subscribe(res => {
      // group.patchValue({...res, sum: +res.price * +res.count});
      // res.sum = +res.price * +res.count;
      console.log(res);
    });

    return group;
  }

You don't have to subscribe the whole form group.您不必订阅整个表单组。 Just look for changes on price and count fields.只需查找价格和计数字段的变化。 Something like below;像下面这样的东西;

 this.group.get('price').valueChanges.subscribe(value => {
      this.price = value;
      this.calculateSum();
  });

this.group.get('count').valueChanges.subscribe(value => {
      this.count = value;
      this.calculateSum();
  });

calculateSum() {
   sum = this.price*this.count;
   this.group.get('sum').setValue(sum);
}

If you subscribe to the whole form and update the value of sum inside value changes there will be an infinite call cycle to value changes.如果您订阅整个表单并在 value 更改中更新 sum 的值,则会有一个无限的调用周期来更改 value。 Instead you should subscribe to individual form controls.相反,您应该订阅单个表单控件。

import { merge } from 'rxjs/observable/merge';

public createService = (): FormGroup => {
   const group = this.fb.group({
     name: [''],
     measure: [''],
     count: [''],
     price: [''],
     sum: ['']
   }); 

   merge(
     group.get('count').valueChanges,
     group.get('price').valueChanges
   ).subscribe(res => {
     this.calculateSum(group);
   });
}

calculateSum(group) {
  const count = +group.get('count').value;
  const price = +group.get('price').value;
  group.get('sum').setValue(count + price);
}
import { combineLatest } from 'rxjs';

   combineLatest(
     this.group.get('count').valueChanges,
     this.group.get('price').valueChanges
   ).subscribe(([count, price]) => {
       this.testForm.get('sum').setValue(+count + +price);
   });

You can set the attribute value for the field sum like {{form.count + form.price}} .您可以为字段sum设置属性,如{{form.count + form.price}} Now I can't test it, but it's something like that.现在我无法测试它,但它是类似的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何订阅反应式FormGroup并映射值更改(角度6) - How to subscribe to a reactive FormGroup and map the value changes, in angular 6 如何访问复杂嵌套 FormGroup 的属性 - How can I access a properties of complex nested FormGroup 如何将formGroup添加到另一个formGroup - How can I add a formGroup to another formGroup 如何将更改对象作为主题发出,并且仅预订一个键更改? - How can I emit an object of changes as a subject and subscribe to only one key changing? 如何访问 FormGroup 中的属性? 我需要访问一些属性(触摸和错误) - How I can access my properties inside the FormGroup? I need to access some properties (touched and errors) 如何订阅两个输入字段并使它们在 Angular 2 中相互切换? - How can I subscribe to two input fields and make them toggle each other in Angular 2? 如何从指令访问 FormGroup - Thinbug - How can I access FormGroup from Directive - Stack Overflow 如何在Angular ControlValueAccessor指令中订阅FormControl值更改 - How Can I Subscribe to FormControl Value Changes In an Angular ControlValueAccessor Directive 如何在角度应用程序中订阅iframe位置更改? - How can I subscribe to iframe location changes in an angular app? 如何订阅表单数组上特定字段的更改 - how can I subscribe to changes on a specific field on a form Array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM