Reverse $ index when listing items in reverse order

I messed up this post on how to reverse display an array, which works great when I try to do it. angular ng-repeat backwards

However ... then the itmes, I am listing editable, and when I change the array, the $ index does not change, so when I edit one of the listed items, the edit occurs on another item that contains the correct index.

So how can I check that my $ index is also canceled?

So the code looks like this:

<form action="" method="post" autocomplete="off" ng-controller="itemCtrl">
 <input type="text" name="createitem" ng-model="item" />
 <button ng-click="addItem(item)">Add item</button>
 
    <ul>
       <li ng-repeat="item in items | reverse">
            <h2>{{item.title}}</h2>
        </li>
    </ul>
</form>
      

Run codeHide result


var itemApp = angular.module('itemApp', []);

itemApp.filter('reverse', function() {
 return function(items) {
  return items.slice().reverse();
 };
});

itemApp.controller('itemCtrl', function ($scope) {

            $scope.items = [];

            $scope.addItem = function(item){
                $scope.items.push({
                    title: item
                });

            };
      

Run codeHide result


+3


source to share


1 answer


This is what I am using. You just need to subtract the length of the array using the current one $index

:



{{myArray.length - $index}}

      

+9


source







All Articles