Amit Shakya
Amit Shakya

Reputation: 994

convert string to Number in angularjs

I am showing an array which contains JSON and am directly showing this with HTML, here you can find elem.fc + elem.cpc. It's giving NaN as an output.

Here is my HTML:

<tr ng-show="table.table1loaded" ng-repeat="elem in filteredcampaignslist" ng-init="initializepopover()">
<td ng-show="app.ostype=='iOS'"><div class="text-center">{{elem.fc+elem.cpc}}</div></td>
</tr>

Upvotes: 1

Views: 52394

Answers (3)

trees_are_great
trees_are_great

Reputation: 3911

This is the answer for versions 2 and above of angular:

Number('1234') // 1234
Number('9BX9') // NaN

Upvotes: 1

Christian Wico
Christian Wico

Reputation: 112

Please check this: How to parseInt in Angular.js

According to the Angular docs within that post, you cannot, as of the moment, perform such operations within expressions.

The best way to go around this is to create a function that does so within a controller and then bind it to your HTML.

HTML:

 <tr ng-show="table.table1loaded" ng-repeat="elem in filteredcampaignslist" ng-init="initializepopover()">
<td ng-show="app.ostype=='iOS'"><div class="text-center">{{ Total() }}</div></td>
</tr>

Inside AngularJS Controller:

$scope.Total = function() { return parseInt(elem.fc) + parseInt(elem.cpc); };

Upvotes: 5

theharls
theharls

Reputation: 163

Change {{elem.fc+elem.cpc}} to {{+elem.fc + +elem.cpc}} That should do the trick.

Upvotes: 5

Related Questions