AngularJS: do not pass $index in ng-repeat if you use orderBy

If you pass $index in ng-repeat when you use orderBy you can perform some operation (“delete” for example) with wrong item. Examplanation below.

Incorrect

HTML:
<div ng-repeat="subtask in currentTask.subtasks | orderBy:'name'">
<button ng-click="remove($index)">Remove subtask</button>
</div>

JS:

$scope.remove = function(idx) {   //<--- idx - is index of subtask in sorted (!!!) array, it can be not the actual index of subtask
   // so you can remove not the subtasks you selected
   var st = $scope.currentTask.subtasks[idx];
   // remove from DB
   SubTask.remove({'subtaskId': st.id});
   // remove from local array
   $scope.subtasks.splice(idx,1);
}

Correct

HTML:

<div ng-repeat="subtask in currentTask.subtasks | orderBy:'name'">
<button ng-click="remove(subtask)">Remove subtask</button>
</div>

JS:

$scope.remove = function(subtask) {
    var idx = $scope.subtasks.indexOf(subtask);
    var st = $scope.currentTask.subtasks[idx];
    // remove from DB
    SubTask.remove({'subtaskId': subtask.id});
    // remove from local array
    $scope.subtasks.splice(idx,1)
}