Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular data binding returning NaN when value is zero

Tags:

angularjs

I'm doing some simple data binding like so:

<input type="text" class="form-control" model="amount">
<label>Your amount is {{amount * 10 }}</label>

However, initially, when the text input is empty it returns NaN.

How can I prevent this from happening with Angular?

like image 442
Billy Assim Avatar asked Aug 30 '26 19:08

Billy Assim


1 Answers

You can try this:

<input type="text" class="form-control" model="amount">
<label>Your amount is {{ (+amount) * 10 }}</label>

HTML text inputs are text by definition. the added + will convert it to an number prior to being used.

like image 62
Sander Elias Avatar answered Sep 02 '26 14:09

Sander Elias