How to calculate value from multiple inputs using angular reactive forms?

杀马特。学长 韩版系。学妹 提交于 2021-01-28 08:49:29

问题


I'm trying to calculate either discount percent or discount amount from list price using angular 6 reactive forms .

form.component.html (simplified)

...
  <form [formGroup]="productForm">
    <mat-form-field class="quat-width">
      <span matPrefix>$ &nbsp;</span>
      <input matInput type="number" name="CostPrice" formControlName="CostPrice">
    </mat-form-field>
    <mat-form-field class="quat-width">
      <span matPrefix>$ &nbsp;</span>
      <input matInput type="number" name="ListPrice" formControlName="ListPrice">
    </mat-form-field>
    <mat-form-field class="quat-width">
      <span matPrefix>$ &nbsp;</span>
      <input matInput type="number" name="DiscountAmount" formControlName="DiscountAmount">
    </mat-form-field>
    <mat-form-field class="quat-width">
      <span matPrefix>% &nbsp;</span>
      <input matInput type="number" name="DiscountPercent" formControlName="DiscountPercent">
    </mat-form-field>
  </form>

form.component.ts (simplified)

...
  export class ProductFormComponent implements OnInit {

    productForm: FormGroup;

    constructor(
      private fb: FormBuilder,
      private productService: ProductsService) { }

    ngOnInit() {
      this.createForm();
    }

    createForm() {
      this.productForm = this.fb.group({
        DiscountAmount: [0],
        DiscountPercent: [0],
        DiscountPrice: [0],
        ListPrice: [0],
      });
      this.productForm.valueChanges.debounce(250).subscribe(val =>{
        if (val.ListPrice > 0 && val.DiscountAmount > 0) {
          const newVal = Math.round((val.ListPrice / val.DiscountAmount) * 100);
          this.productForm.controls.DiscountPercent.patchValue(newVal);
        }
        if (val.ListPrice > 0 && val.DiscountAmount > 0) {
          const newVal = Math.round((discPercent / 100) * val.ListPrice);
          this.productForm.controls.DiscountAmount.patchValue(newVal);
        }
      }  
      this.validateForm();
    }

    validateForm() {
      Object.keys(this.productForm.controls).forEach(key => {
        this.productForm.controls[key].markAsTouched();
        this.productForm.controls[key].updateValueAndValidity();
      });
    }
  }

the code above is what I got so far and doesn't work, creates a maximum callstack reached error.

How can I set the value of discount amount if list price and discount percent is changed, and set the value of the discount percent when list price and discount amount is changed?


回答1:


you can use {emitEvent: false} option for patchValue and updateValueAndValidity

 this.productForm.valueChanges.subscribe(val =>{
        if (val.ListPrice > 0 && val.DiscountAmount > 0) {
          const newVal = Math.round((val.ListPrice / val.DiscountAmount) * 100);
          this.productForm.controls.DiscountPercent.patchValue(newVal, {emitEvent: false});
        }
        if (val.ListPrice > 0 && val.DiscountAmount > 0) {
          const newVal = Math.round((val.DiscountPercent / 100) * val.ListPrice);
          this.productForm.controls.DiscountAmount.patchValue(newVal, {emitEvent: false});
        }
       this.validateForm();   
      });


    }

    validateForm() {
      Object.keys(this.productForm.controls).forEach(key => {
        this.productForm.controls[key].markAsTouched();
        this.productForm.controls[key].updateValueAndValidity({emitEvent: false});
      });
    }


来源:https://stackoverflow.com/questions/51005666/how-to-calculate-value-from-multiple-inputs-using-angular-reactive-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!