Reputation: 72
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!
Upvotes: 3
Views: 14975
Reputation: 9
{{" - " + (priceInfo.adjustment_base_price2)}}
make sure expression is inside the (brackets)
Upvotes: -1
Reputation:
In some extreme cases like mine, where none of the above solutions work, there is one saviour solution : subtract 0 and then perform the addition.
So your addition expression
{{ (currentPage|number)+(1|number) }}
could be re-written as
{{ (currentPage|number) - 0 +(1|number) }}
To further explain, the expression is forcibly made to appear as a number through the subtraction of 0.
Upvotes: 3
Reputation: 760
you can just Do like this :
{{ (currentPage|number)-(-1|number) }} instead of {{ (currentPage|number)+(1|number) }}
Upvotes: 1
Reputation: 11
I tried parseFloat() but for some reason that didn't work. But since using the - will default to a math operation, instead of adding (+) I subtracted a negative.
$scope.add = function() {$scope.counter += $scope.amount;} // doesn't work
$scope.add = function() {$scope.counter -= -$scope.amount;} // works perfectly.
Upvotes: 1
Reputation: 7586
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
Upvotes: 1
Reputation: 6541
As you can see in the docs, the number filter returns a string. The +
operator is the concatenation operation in String.
You have to parse the string into number by yourself.
Something like this:
.filter('toNumber', function() {
return function(n) {
return new Number(n);
};
});
Upvotes: 0