Answer The ng-repeat directive has a set of special variables which you are useful while iterating the collection. These variables are as follows:
- $index
- $first
- $middle
- $last<html>
<head>
<script src="lib/angular.js"></script>
<script>
var app = angular.module("app", []);
app.controller("ctrl", function ($scope) {
$scope.friends = [
{ name: 'shailendra', gender: 'boy' },
{ name: 'kiran', gender: 'girl' },
{ name: 'deepak', gender: 'boy' },
{ name: 'pooja', gender: 'girl' }
];
});
</script>
</head>
<body ng-app="app">
<div ng-controller="ctrl">
ul class="example-animate-container">
<li ng-repeat="friend in friends">
[{{$index + 1}}] {{friend.name}} is a {{friend.gender}}.
<span ng-if="$first">
(first element found) </span>
<span ng-if="$middle"> (middle element found) </span>
<span ng-if="$last"> (last element found) </span>
</li>
</ul>
</div>
</body>
</html>
The $index contains the index of the element being iterated. The $first, $middle and $last returns a boolean value depending on whether the current item is the first, middle or last element in the collection being iterated.