How can I parse String to Int in an Angular expression?
Solution 1:
In your controller:
$scope.num_str = parseInt(num_str, 10); // parseInt with radix
Solution 2:
I prefer to use an angular filter.
app.filter('num', function() {
return function(input) {
return parseInt(input, 10);
};
});
then you can use this in the dom:
{{'10'|num}}
Here is a fiddle.
Hope this helped!
Solution 3:
You can try:
{{ 1 * num_str + 1 }}
http://jsfiddle.net/Z32fP/
Solution 4:
Another option would be:
$scope.parseInt = parseInt;
Then you could do this like you wanted:
{{parseInt(num_str)-1}}
This is because angular expressions don't have access to the window
, only to scope
.
Also, with the number filter, wrapping your expression in parentheses works:
{{(num_str-1) | number}}
DEMO