Need for product_price to be a number

23 views
Skip to first unread message

Maureen Moore

unread,
Jul 17, 2020, 7:33:43 AM7/17/20
to Angular and AngularJS discussion
When I get the product_price it returns "Unsupported value type" as a result of the function below but I need for it to be a number. I tried this.product_price = parseFloat(this.product_price); but that returns the error: Argument of type 'number' is not assignable to parameter of type 'string'. How do I cast product_price as a number?

In my HTML:

<div>{{ totalPrice }}</div>
<ul *ngFor="let product of products">
 
<li>
 
<div>{{product.price }}</div>
 
<button type="submit" class="btnAddAction" (click)="onSubmit( [product.price] )">Add to Cart</button>
 
</li>
</ul>



The HTML is getting the products from the database which was created with this JSON:

{
 
"products": [
   
{
     
"price": 20.50,
   
},
   
{
     
"price": 20.50,
   
},
   
{
     
"price": 12.75,
   
}
 
]
}





In my controller typescript file:

get totalPrice()  {
if (typeof this.product_price === "string") {
   
return "string";
}
if (typeof this.product_price === "number") {
   
return "number";
} else {
   
throw Error("Unsupported value type")
}
}
product_price
: number;


onSubmit
(product_price){
product_price
= parseFloat(product_price);
const data = {
   product_price
};
this.items.push(data);
localStorage
.setItem('items', JSON.stringify(this.items));
}



Lucas Lacroix

unread,
Jul 17, 2020, 8:30:19 AM7/17/20
to ang...@googlegroups.com
Hi Maureen,
I do not see where you set the product_price variable. Did you forget to initialize it's value?

-Luke

--
You received this message because you are subscribed to the Google Groups "Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/angular/86dbd802-60fb-45d1-8450-1a8fcc744360o%40googlegroups.com.


--
Lucas Lacroix
Computer Scientist
RESTful APIs & Cloud Platform
Advanced Technology Division, MEDITECH

                
Subscribe to receive emails from MEDITECH or to change email preferences.

Maureen Moore

unread,
Jul 17, 2020, 8:33:57 AM7/17/20
to Angular and AngularJS discussion
I set the product_price in the onSubmit function.


Lucas Lacroix

unread,
Jul 17, 2020, 8:37:39 AM7/17/20
to ang...@googlegroups.com
Hi Maureen,
In the submit function, you have "product_price =". That will set the variable passed into the function to a new value. If you want to set the variable used by the HTML template, you need "this.product_price =" instead.

-Luke

On Fri, Jul 17, 2020 at 8:34 AM Maureen Moore <maka...@gmail.com> wrote:
I set the product_price in the onSubmit function.


--
You received this message because you are subscribed to the Google Groups "Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.

Maureen Moore

unread,
Jul 17, 2020, 9:08:48 AM7/17/20
to Angular and AngularJS discussion
Thank you, Lucas. That totally worked and it's fixed now.
Reply all
Reply to author
Forward
0 new messages