2

Let's say I have the following array

$scope.stuff = [
    {name: "one", order: 1},
    {name: "three", order: 3},
    {name: "two", order: 2}
]

Then I list it using ng-repeat like so:

<div ng-repeat="(key, data) in stuff | orderBy:'-order'">
    {{ data.name }} - {{ key }} - {{ $index }}
    <br />
</div>

It then would display:

1 - 0 - 0
2 - 1 - 1
3 - 2 - 2

I'd like to be able to know where in the ACTUAL array, these items are coming from in $scope.stuff

So I'd like to know that 3 is actually in array index 1 in $scope.stuff and not just array index 2 in my sorted ng-repeat.

I'm new to angular but does anyone have any suggestions?

bryan
  • 8,879
  • 18
  • 83
  • 166

1 Answers1

4

I dont think ng-repeat has anything to get this functionality, but i used the array indexof method to get the actual index.

 <div ng-repeat="(key, data) in stuff | orderBy:'-order'">
    {{ data.name }} - {{ key }} - {{ $index }} - {{stuff.indexOf(data)}}
    <br />
</div>

http://plnkr.co/edit/xjTZ6Y3xFqs6Bs3LGJk6

Kathir
  • 6,136
  • 8
  • 36
  • 67
  • What's the browser compatibility of angular's indexOf? – bryan May 14 '15 at 18:13
  • ah okay, thanks Kathir. So I guess it would be safe to assume that I can use [this](http://stackoverflow.com/a/9768663/2761425) to make sure it works in ie8? – bryan May 14 '15 at 18:16
  • I dont think ie8 implements this functionality. http://stackoverflow.com/questions/1744310/how-to-fix-array-indexof-in-javascript-for-internet-explorer-browsers you can follow the answer from that question to fix it across. – Kathir May 14 '15 at 18:21