<body ng-app="myModule">
<div ng-con troller="myController">
<table>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Gender</th>< /span>
<th>Salary</th>< /span>
</tr>
</thead>
<tbody>
<tr ng-repeat="employee in employees">
<td> {{ employee.firstName }} </td>
<td> {{ employee.lastName }} </td>
<td> {{ employee.gender }} &l t;/td>
<td> {{ employee.salary }} &l t;/td>
</tr>
</tbody>
</table>
</div></ span>
</body>
</html>
Nested ng-repeat example : The model contains an array of countries, and each country has an array of cities. The view must display cities nested under their respective country.
Script.js : The model is an array of countries. Each country contains an array of cities.
var app = angular
.module("myModule", [])
.controller("myController", function($scope) {
var countries = [
{
name: "UK",
cities: [
{ name: "London" },
{ name: "Birmingham" },
{ name: "Manchester" }
]
},
{
name: "USA",
cities: [
{ name: "Los Angeles" },
{ name: "Chicago" },
{ name: "Houston" }
]
},
{
name: "India",
cities: [
{ name: "Hyderabad" },
{ name: "Chennai" },
{ name: "Mumbai" }
]
}
];
$scope.countries = countries;
});
HtmlPage1.html : Notice that we are using two ng-repeat directives in the view, one nested inside the other. The outer ng-repeat directive loops thru each country in the model. The inner ng-repeat directive loops thru each city of a given country.
<!DOCTYPE html>
<html xmlns="http://www.w3.org /1999/xhtml">
<head>
<title>& lt;/title>
<script src ="Scripts/angular.min.js"></script>
<script src ="Scripts/Script.js"></script>
</head>
<body ng-app="myModule">
<div ng-con troller="myController">
<ul ng-repeat="country in countries">
<li>
{{country.name}}
<ul>
<li ng-repeat="city in country.cities">
{{city.name}}
</li>
</ul>
</li>
</ul>
</div></ span>
</body>
</html>
Finding the index of an item in the collection :
- To find the index of an item in the collection use $index property
- To get the index of the parent element
- Use $parent.$index or
- Use ng-init="parentIndex = $index"
The following example, shows how to retrive the index of the elements from a nested collection
<!DOCTYPE html>
<html xmlns="http://www.w3.org /1999/xhtml">
<head>
<title>& lt;/title>
<script src ="Scripts/angular.min.js"></script>
<script src ="Scripts/Script.js"></script>
</head>
<body ng-app="myModule">
<div ng-con troller="myController">
<ul ng-repeat="country in countries" ng-init="parentIndex = $index">
<li>
{{country.name}} - Index = {{ $index }}
<ul>
<li ng-repeat="city in country.cities">
{{city.name}} - Parent Index = {{ parentIndex }}, Index = {{ $index }}
</li>
</ul>
</li>
</ul>
</div>
</body>
</html>
0 comments :
Post a Comment