AngularJS Expression Concatenating Instead of Adding

Issue

I have the following 2 expressions in my HTML (as the param for a controller function called within the ngClick directive), where I am trying to implement simple paging functionality:

{{ (currentPage|number)+(1|number) }} – This concatenates the two values together as if they were a string.

{{ (currentPage|number)-(1|number) }} – This calculates the correct and expected numeric result.

Any idea what the problem is here? I have tried various ways to add a literal and AngularJS value together to no avail. I am using version 1.2.0.

Thanks in advance!

Solution

Since + is an addition and concatenation operator the concatenation is taking precedence whereas - is only subtraction. Try using parseInt() wherever you calculate the number value or within the expression.

submitted as it worked

Answered By – SamV

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published