top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What are ng-repeat special variables?

0 votes
71 views
What are ng-repeat special variables?
posted Oct 20, 2017 by Sathaybama

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

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">
 <div>
 [{{$index + 1}}] {{friend.name}} is a {{friend.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>

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.

answer Oct 20, 2017 by Shivaranjini
...