top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What are ng-repeat special variables?

0 votes
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

 <script src="lib/angular.js"></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' }
<body ng-app="app">
 <div ng-controller="ctrl">
 <ul class="example-animate-container">
 <li ng-repeat="friend in friends">
 [{{$index + 1}}] {{}} is a {{friend.gender}}.
 <span ng-if="$first">
 <strong>(first element found)</strong>
<span ng-if="$middle">
 <strong>(middle element found)</strong>
<span ng-if="$last">
 <strong>(last element found)</strong>

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