0

I have been creating a billing application where I will be capturing item name and price. I want to give discount for the item so I will be having discount in percentage and discount in dollar text boxes. In some cases, either I can enter discount in percentage or discount in dollar values so I want to display both of the them at run time. For example, if I enter discount in dollar then I have to display value in discount in percentage. Can you please help me to solve the requirement?

I have created a sample application using Stackblitz with Angular and reactive forms - https://stackblitz.com/edit/github-hayn68-yjqorz

1 Answer 1

1

Try this:

<td class="form-group">
      <input #perc type="text" class="form-control" formControlName="discountInPercentage" 
      (change)="percChanged(i, perc.value)">
</td>

TS

percChanged(i, val){
    this.itemForm.value.items[i].discountInDollar = this.itemForm.value.items[i].rate/100*val;
    this.itemForm.controls['items'].setValue(this.itemForm.value.items);
    console.log(this.itemForm.value.items[i].discountInDollar);
}

Change the %Discount value and you should see the output. Stackblitz example

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you @ala. Can you please suggest on updating the value in UI. I had formControlName="discountInDollar" in the template but the value in not getting updated after percChanged method set the value this.itemForm.value.items[i].discountInDollar. Updated stackblitz URL - stackblitz.com/edit/github-hayn68-ovpgar
a work around would be to add this this.itemForm.controls['items'].setValue(this.itemForm.value.items); to your percChanged, stackblitz.com/edit/github-hayn68-m6deot
I edited my answer, kindly upvote and mark as solved if that solved your issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.