Part 6 AngularJS ng repeat directive
ng-repeat is similar to foreach loop in C#.
Let us understand this with an example. Here is what we want to do.
1.
For each employee we have in the employees array we want a table row.
With in each cell of the table row we to display employee
- Firstname
- Lastname
- Gender
- Salary
This can be achieved very easily using ng-repeat directive
Script.js : The controll function builds the model for the view. The model employees has the list of all employees.
var app = angular
.module("myModule", [])
.controller("myController", function ($scope) { var employees = [
{ firstName: "Ben", lastName: "Hastings", gender: "Male", salary: 55000 },
{ firstName: "Sara", lastName: "Paul", gender: "Female", salary: 68000 },
{ firstName: "Mark", lastName: "Holland", gender: "Male", salary: 57000 },
{ firstName: "Pam", lastName: "Macintosh", gender: "Female", salary: 53000 },
{ firstName: "Todd", lastName: "Barber", gender: "Male", salary: 60000 }
]; $scope.employees = employees;
});
HtmlPage1.html : In the view, we are using ng-repeat directive which loops thru each employee in employees array. For each employee, we a get a table row, and in each table cell of the table row, the respective employee details (Firstname, Lastname, Gender, Salary) are retrieved using the angular binding expression
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="Scripts/angular.min.js"></script>
<script src="Scripts/Script.js"></script>
</head>
<body ng-app="myModule">
<div ng-controller="myController">
<table>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Gender</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="employee in employees">
<td> {{ employee.firstName }} </td>
<td> {{ employee.lastName }} </td>
<td> {{ employee.gender }} </td>
<td> {{ employee.salary }} </td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
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;
});
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="Scripts/angular.min.js"></script>
<script src="Scripts/Script.js"></script>
</head>
<body ng-app="myModule">
<div ng-controller="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>
</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></title>
<script src="Scripts/angular.min.js"></script>
<script src="Scripts/Script.js"></script>
</head>
<body ng-app="myModule">
<div ng-controller="myController">
<ul ng-repeat="country in countries" ng-init="parentIndex = $index"> using ng-init
<li>
{{country.name}} - Index = {{ $index }}
<ul>
<li ng-repeat="city in country.cities">
{{city.name}} - Parent Index = {{ parentIndex }}, Index = {{ $index }}
</li>
</ul>
</li>
</ul>
<ul ng-repeat="country in countries">
<li>
{{country.name}} - Index = {{ $index }}
<ul>
<li ng-repeat="city in country.cities"> using $parent.$index
{{city.name}} - Parent Index = {{ $parent.$index }}, Index = {{ $index }}
</li>
</ul>
</li>
</ul>
</div>
</body> </html>
Part 6 AngularJS ng repeat directive的更多相关文章
- Part 15 AngularJS ng init directive
The ng-init directive allows you to evaluate an expression in the current scope. In the following e ...
- part 4 AngularJS ng src directive
- AngularJs学习笔记--directive
原版地址:http://code.angularjs.org/1.0.2/docs/guide/directive Directive是教HTML玩一些新把戏的途径.在DOM编译期间,directiv ...
- angularjs中的directive scope配置
angularjs中的directive scope配置 定义directive其中重要的一环就是定义scope,scope有三种形式: 默认的scope,DOM元素上原有的scope scope: ...
- AngularJS: 'Template for directive must have exactly one root element' when using 'th' tag in directive template
.controller('HomeController', function($scope,$location) { $scope.userName='天下大势,为我所控!'; $scope.clkU ...
- angularJs中自定义directive的数据交互
首先放官方文档地址:https://docs.angularjs.org/guide/directive 就我对directive的粗浅理解,它一般用于独立Dom元素的封装,应用场合为控件重用和逻辑模 ...
- angularjs自定义指令Directive
今天学习angularjs自定义指令Directive.Directive是一个非常棒的功能.可以实现我们自义的的功能方法. 下面的例子是演示用户在文本框输入的帐号是否为管理员的帐号"Adm ...
- AngularJS中有关Directive的汇总
本篇通过几个例子对AngularJS中的Directive进行汇总. 例子1,单向绑定和双向绑定 <html ng-app="myApp"> <head> ...
- AngularJS自定义指令directive:scope属性 (转载)
原文地址:http://blog.csdn.net/VitaLemon__/article/details/52213103 一.介绍: 在AngularJS中,除了内置指令如ng-click等,我们 ...
随机推荐
- CentOS 6.5 源码安装MySQL5.6
1:下载安装cmake (mysql5.5以后是通过cmake来编译的) #http://download.csdn.net/detail/csxuedn/7976005 #wget http://w ...
- Android Studio开发JNIproject
使用Android Sutdio创建一个新的project后,接下来记录创建NDKproject的基本步骤. 本文将达到: 1. 创建NDKproject 2. 在JNI中输出Log语句 3. 指定编 ...
- Cocos2dx Widget button透明区域过滤
小伟哥 遇到一个命题: button透明区域过滤.当点击一个建筑button.花的时候不得不想一些方法把点击透明区域过滤掉. 让点击也没有效果滴啦. 開始搜索了半天才有所思路. 在网络上非常多贴代码的 ...
- IOS 7 Study - UISegmentedControl
You would like to present a few options to your users from which they can pick anoption, through a U ...
- Codeforces Round #336 (Div. 2) C. Chain Reaction set维护dp
C. Chain Reaction 题目连接: http://www.codeforces.com/contest/608/problem/C Description There are n beac ...
- FluorineFx对于现有站点的配置
step 1:新建一个FluorineFX网站,作为参考 step 2:在现有网站添加FluorineFX网站的相关dll引用,并拷贝console.aspx和gateway.aspx至网站根目录(最 ...
- android startActivityForResult的用法
有时候我们需要把A activity提交数据给B activity处理,然后把结果返回给A 这种方式在很多种情况需要用到,比如我应用的程序需要有拍照上传的功能. 一种解决方案是 我的应用程序 〉调 ...
- PHP __autoload函数(自动载入类文件)的使用方法(转)
详细出处参考:http://www.jb51.net/article/29625.htm 在使用PHP的OO模式开发系统时,通常大家习惯上将每个类的实现都存放在一个单独的文件里,这样会很容易实现对类进 ...
- 分布式存储系统sheepdog
Sheepdog,是由NTT的3名日本研究员开发的开源项目,主要用来为虚拟机提供块设备. 其架构例如以下: 以下,我们将从架构.模块等几个方面来介绍下: 一.架构图 如上图: 採用无中心节点的全对称架 ...
- Java元组Tuple使用实例--转载
原文地址:http://50vip.com/35.html 一.为什么使用元组tuple? 元组和列表list一样,都可能用于数据存储,包含多个数据:但是和列表不同的是:列表只能存储相同的数据类型,而 ...