Skip to content Skip to sidebar Skip to footer

How Do I Get The $index Of Element In An Ng-repeat When Repeating Over An Object?

I know object values don't have indexes but say I have an object named foo with 5 keys and I do:
I will end up with 5 divs on my page. M

Solution 1:

You could still use {{$index}} when repeating non array objects.

Markup:

<div ng-app>
  <div ng-controller="testCtrl">
    <div ng-repeat="(key, value) in data">
      <span>{{$index}} -</span>
      <span>{{key}} -</span>
      <span>{{value}}</span>
    </div>
  </div>
</div>

Controller

function testCtrl($scope) {

  $scope.data = {
    prop1: 'a',
    prop2: 'b',
    prop3: 'c',
    prop4: 'c'
  }

}

Output:

0 - prop1 - a
1 - prop2 - b
2 - prop3 - c
3 - prop4 - c

See this fiddle for your reference


Post a Comment for "How Do I Get The $index Of Element In An Ng-repeat When Repeating Over An Object?"