1

I am working with Angular and I am trying to do this

<td>{{(obj.revenue.total / obj.clicks.total).toFixed(2)}}</td>

and in the table the result is something like this

**EPC** 0.60 NaN // returns this, why ?

I prepared a JSFiddle where you will see this

<table>
  <tr>
    <th ng-repeat='header in headers'>{{header}}</th>
  </tr>
  <tr ng-repeat='obj in data'>
    <td></td>
    <td>{{obj.clicks.total.toFixed(2)}}</td>
    <td>{{obj.landing_pages.total_clicks.toFixed(2)}}</td>
    <td>{{obj.landing_pages.click_through_rate.toFixed(2)}}</td>
    <td>{{obj.conversions.total.toFixed(2)}}</td>
    <td>{{(obj.conversions.total / obj.landing_pages.total_clicks)}}</td>
    <td>{{obj.conversions.amount.toFixed(2)}}</td>
    <td>{{obj.cost.total.toFixed(2)}}</td>
    <td>{{(ob.conversions.amount - obj.cost.total).toFixed(2)}}</td>
    <td>{{obj.net.roi.toFixed(2)}}</td>
    <td>{{obj.cost.cpc}}</td>
    <td>{{(obj.revenue.total / obj.clicks.total).toFixed(2)}}</td>
    <td>{{obj.cost.ecpc.toFixed(2)}}</td>
  </tr>
</table>

and the controller

.controller('PeopleCtrl', function($scope) {
  $scope.headers = [
    'Traffic Source',
    'Clicks',
    'LP Clicks',
    'LP CTR',
    'Conv',
    'CVR',
    'Rev',
    'Spend',
    'Profit',
    'ROI',
    'CPC',
    'EPC',
    'EPA'
  ];

  $scope.data = [];

  $scope.LoadMyJson = function() {
    angular.forEach(myJson, function(items) {
      $scope.data.push(items)
    })        
  };
  $scope.LoadMyJson();

})

http://jsfiddle.net/x5hfwdfs/

see the tds CVR, Profit and EPC, are the ones where I need to calculate.

Do you guys have an idea of the issue ?

4
  • You should probably validate that the properties exist and have the types of values you expect before they go into the view. Commented Jan 25, 2016 at 20:07
  • you could use filter: fractionSize: 2 Commented Jan 25, 2016 at 20:08
  • @elclanrs can you give me a piece of code? all of them expect to be numbers, except the header Traffic Source, that is a name. Commented Jan 25, 2016 at 20:09
  • 1
    Well, what I mean is that an expression like (obj.revenue.total / obj.clicks.total).toFixed(2) can fail in various ways. In the controller I'd check if these properties exist first like if (obj.revenue && obj.revenue.total && typeof obj.revenue.total === 'number') $scope.revenueTotal = obj.revenue.total and use that in the view so you know if it displays something is always what you expect, otherwise fail with an error, or gracefully, like display a zero if it is supposed to be a number. Commented Jan 25, 2016 at 20:12

1 Answer 1

1

You can use the fractionSize filter:

<td>{{obj.clicks.total | fractionSize: 2}}</td>

You can see the example here:

https://docs.angularjs.org/api/ng/filter/number

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, it fix it but I am having something wrong, jsfiddle.net/kvat53Lv , look at Profit, there is a 0.00 below the 1st row, why ?
You have an extra string in your data. It's producing 3 rows, not two as expected.
"name": "Traffic Source #345"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.