-5

For example lets take an array example

$scope.users = ['John','Peter'];

How to get the index of John ?

Charlie
  • 21,138
  • 10
  • 54
  • 85
Sethu Raja
  • 29
  • 5

3 Answers3

2
$scope.users.indexOf('John')

This is not angular specific. This is a JS array method.

Charlie
  • 21,138
  • 10
  • 54
  • 85
0

try this in your view html {{users.indexOf('John')}} or in your controller use $scope.users.indexOf('John')

Hayot
  • 251
  • 6
  • 15
-1

Use track by $index.

E.g. <p ng-repeat = "user in $scope.users track by $index"></p>

Then you can determine if it's John:

<p ng-repeat = "user in $scope.users track by $index">
  <span ng-if="$scope.users[$index] === 'John'">Here's John at {{$index}}</span>
</p>
Katana24
  • 8,153
  • 17
  • 72
  • 112