Hello Readers,
Hope you are doing good today.
Today on My blog, I am going to explain about the ng-repeat special variables in AngularJS. The nag-repeat directive has a set of special variables which can be used while iterating the collection.
These Special variables are:
$index
$first
$middle
$last
In below code we are iterating the collection of friends and checking the position of value with the help of ng-repeat special variables.
<div ng-app="app" ng-controller="ctrl">
<ul>
<li ng-repeat="friend in friends">
//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.
<div>
<span ng-if="$first">
<strong>{{friend.name}} (first element found)</strong>
</span>
<span ng-if="$middle">
<strong>{{friend.name}} (middle element found)</strong>
</span>
<span ng-if="$last">
<strong>{{friend.name}} (last element found)</strong>
</span>
</div>
</li>
</ul>
</div>
In below script we create the friends array.
var app = angular.module("app", []);
app.controller("ctrl", function ($scope) {
$scope.friends = [{ name: 'Rafi', gender: 'boy' },
{ name: 'Kajal', gender: 'girl' },
{ name: 'deepak', gender: 'boy' },
{ name: 'sonam', gender: 'girl' }];
});
I hope this will help you. Please feel free to give us your feedback in comments.
0 Comment(s)