Angular JS Magazine

duminică, 24 august 2014

Working with the Built-in Variables

The ng-repeat directive contains several built-in variables:

$index - returns the position of the current object or property
$first - returns true if the current object is the first in the collection
$middle - returns true if the current object is neither the first nor last in the collection
$last - returns true if the current object is the last in the collection
$even - returns true for the even-numbered objects in a collection
$odd - returns true for the odd-numbered objects in a collection

Example of $index:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="../angular/angular.min.js"></script>
    <link href="../bootstrap/css/bootstrap.css" rel="stylesheet" />
    <link href="../bootstrap/css/bootstrap-theme.css" rel="stylesheet" />
</head>
<body>
<div ng-app="" ng-init="atp=[
     { name: 'Nadal, Rafael (ESP)', rank: 1 },
     { name: 'Djokovic, Novak (SRB)', rank: 2 },
     { name: 'Federer, Roger (SUI)', rank: 3 },
     { name: 'Wawrinka, Stan (SUI)', rank: 4 },
     { name: 'Ferrer, David (ESP)', rank: 5 }]">

    <div id="atpPanel" class="panel">
        <h3 class="panel-header">ATP SINGLES RANKINGS</h3>
        <table class="table">
            <thead>
            <tr>
                <th>No.</th>
                <th>Name</th>
                <th>Rank</th>
            </tr>
            </thead>
            <tbody>
            <tr ng-repeat="item in atp">
                <td>{{$index + 1}}</td>
                <td>{{item.name}}</td>
                <td>{{item.rank}}</td>
            </tr>
            </tbody>
        </table>
    </div>
</div>
</body>
</html>