Jump to content
Sign in to follow this  
jazzyscript

Angular - can't add variables

Recommended Posts

I am trying to make a total that accumulates, like so:

 

$scope.total = 0;

...

$scope.total += $scope.amountIn;

 

But even when amountIn is a number, the values are concatenated. Yet if I try a multiplication operation like so:

 

$scope.total = 10 * $scope.amountIn;

 

It does the math! I've never had this problem with regular javascript. Help?

Share this post


Link to post
Share on other sites

Make sure that both arguments are numbers, or else it will concatenate. If it's concatenating then one of those arguments is not a number.

Share this post


Link to post
Share on other sites

I fixed it. Had to learn some HTML5.

 

It wasn't a number because I was using input type="text". I thought it would do that conversion automatically.

 

I fixed it with <input type="number" step="0.1"> since I want users to be able to enter floats.

Share this post


Link to post
Share on other sites

You'll still want to convert the value to a number, there are still a lot of browsers in use that don't use the newer HTML 5 field types. They will still be text.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...