Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to print values computed from variables in angular?

I have a FormGroupin a component like:

form: FormGroup = new FormGroup({
    $key: new FormControl(null),
    from: new FormControl('', Validators.required),
    to: new FormControl('', Validators.required),
    quantity: new FormControl('', [Validators.required, Validators.pattern('[0-9]*')]),
    price: new FormControl('', [Validators.required, Validators.pattern('[0-9]*')]),
    ticker: new FormControl('', Validators.required)
  });

And in the html, I have the Input elements to accept the inputs for each of the five form-controls.

I’m just facing some trouble in outputting Amount to the html:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<p>Amount: {{ form.controls['quantity'] * form.controls['price']}}</p>

I’m getting errors:

The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.

The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.

Can someone help me to understand whats going on?

>Solution :

Convert the variables to numbers.

Try this:

<p>Amount: {{ +form.controls['quantity'] * +form.controls['price']}}</p>

Or change type of your input to number:

<input type="number" formControlName="quantity">
<input type="number" formControlName="price">

And change your form:

quantity: new FormControl(null, Validators.required),
price: new FormControl(null, Validators.required),
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading