The ng-repeat directive has a set of special variables, which you are useful while iterating the collection.
- $index
- $first
- $middle
- $last
<head>
<script src="angular.js"></script>
<script>
var app = angular.module("app", []);
app.controller("ctrl", function($scope)
{
$scope.employees = [
{
name: 'jithil',
gender: 'boy'
},
{
name: 'jibi',
gender: 'girl'
},
{
name: 'betty',
gender: 'boy'
},
{
name: 'johnson',
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>Output:
jithil is a boy. (first element found)
jibi is a girl. (middle element found)
betty is a girl. (middle element found)
johnson is a boy. (last element found)
