The ng-repeat directive has a set of special variables, which you are useful while iterating the collection.
- $index
- $first
- $middle
- $last
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<head>
<script>
var app = angular.module("app", []);
app.controller("ctrl", function ($scope)
{
$scope.employees = [
{
name: 'A',
gender: 'boy'
},
{
name: 'B',
gender: 'girl'
},
{
name: 'C',
gender: 'boy'
},
{
name: 'D',
gender: 'boy'
}];
});
</script>
</head>
<body ng-app="app">
<div ng-controller="ctrl">
<ul>
<li ng-repeat="employee in employees">
<div> {{employee.name}} is a {{employee.gender}}. <span ng-if="$first">
<strong>(first element found)</strong>
</span> <span ng-if="$middle">
<strong>(middle element found)</strong>
</span> <span ng-if="$last">
<strong>(last element found)</strong>
< /span> </div>
</li>
</ul>
</div>
</body>
</html> OutputA is a boy. (first element found)
B is a girl. (middle element found)
C is a boy. (middle element found)
D is a boy. (last element found)
