1

I have a simple table which displays data objects, with one of the properties of the data objects being numbers.

Here's my table with ngRepeat:

<tbody>
    <tr ng-repeat="number in Numbers">
      <td>{{ $index }}</td>
    <td>{{ number.num }}</td>
    </tr>
</tbody>

Now what i'd like to do is add a column that calculates and displays the percent difference between the first and preceding numbers. Is there any way to do this with ngRepeat? I tried adding a column like this:

<td>{{ number.num[$index] / number.num[$index + 1] }}</td>

But in the end it didn't work. It ended up displaying all the data in that column as NaNs. Am I missing something here?

Nick D.
  • 11
  • 3
  • 1
    Think you mean to reference `Numbers[$index].num/Numbers[$index + 1].num` – logee Sep 01 '15 at 01:41
  • I tried that but it seems it doesn't work either. The thing is that the 'num' is actually a property of the Numbers objects, and it's where the actual numbers are stored. I feel like it should still be something after .num. – Nick D. Sep 01 '15 at 01:51

1 Answers1

0

user2341963 has the correct answer:

angular.module('test', [])

.controller('Test', function($scope) {
  var numbers = [1,3,5,53,2,552,32];
  
  $scope.Numbers = numbers.map(function(n) {
    return {num: n};
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.1/angular.min.js"></script>

<div ng-app='test' ng-controller='Test'>
  <div ng-repeat="number in Numbers">
    index: {{$index}} |
    number: {{number.num}} |
    result: {{Numbers[$index].num/Numbers[$index + 1].num}}
  </div>
</div>
Icycool
  • 7,099
  • 1
  • 25
  • 33
  • Thanks a lot Icycool and user2341963, I fiddled around with it a bit more and found out I was using non number values for the first few entries which messed up the whole table, even though the solution was correct. – Nick D. Sep 01 '15 at 05:45