ng-repeat指令非常适合用来显示表格。


在表格中显示数据

  在AngularJS中显示表格非常容易:

<div ng-app="myApp" ng-controller="customersCtrl">
<table>
<tr ng-repeat="x in names">
<td>{{ x.Name }}</td>
<td>{{ x.Country }}</td>
</tr>
</table>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function ($scope) {
$scope.names = [{
"Name": "Alfreds Futterkiste",
"City": "Berlin",
"Country": "Germany"
}, {
"Name": "Ana Trujillo Emparedados y helados",
"City": "México D.F.",
"Country": "Mexico"
}, {
"Name": "Antonio Moreno Taquería",
"City": "México D.F.",
"Country": "Mexico"
}, {
"Name": "Around the Horn",
"City": "London",
"Country": "UK"
}, {
"Name": "B's Beverages",
"City": "London",
"Country": "UK"
}, {
"Name": "Berglunds snabbköp",
"City": "Luleå",
"Country": "Sweden"
}, {
"Name": "Blauer See Delikatessen",
"City": "Mannheim",
"Country": "Germany"
}, {
"Name": "Blondel père et fils",
"City": "Strasbourg",
"Country": "France"
}, {
"Name": "Bólido Comidas preparadas",
"City": "Madrid",
"Country": "Spain"
}, {
"Name": "Bon app'",
"City": "Marseille",
"Country": "France"
}, {
"Name": "Bottom-Dollar Marketse",
"City": "Tsawassen",
"Country": "Canada"
}, {
"Name": "Cactus Comidas para llevar",
"City": "Buenos Aires",
"Country": "Argentina"
}, {
"Name": "Centro comercial Moctezuma",
"City": "México D.F.",
"Country": "Mexico"
}, {
"Name": "Chop-suey Chinese",
"City": "Bern",
"Country": "Switzerland"
}, {
"Name": "Comércio Mineiro",
"City": "São Paulo",
"Country": "Brazil"
}];
});
</script>

运行


加上样式

  为了使上面的表格更好看,我们在页面上加上一些CSS:

<style>
table, th , td {
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
}
table tr:nth-child(odd) {
background-color: #f1f1f1;
}
table tr:nth-child(even) {
background-color: #ffffff;
}
</style>

运行


加上orderBy过滤器

  要对表格数据进行排序,加上orderBy过滤器:

<table>
<tr ng-repeat="x in names | orderBy : 'Country'">
<td>{{ x.Name }}</td>
<td>{{ x.Country }}</td>
</tr>
</table>

运行


加上uppercase过滤器

  要显示大写形式,添加uppercase过滤器:

<table>
<tr ng-repeat="x in names">
<td>{{ x.Name }}</td>
<td>{{ x.Country | uppercase }}</td>
</tr>
</table>

运行


显示行号($index)

  要显示行号,在<td>中添加$index

<table>
<tr ng-repeat="x in names">
<td>{{ $index + 1 }}</td>
<td>{{ x.Name }}</td>
<td>{{ x.Country }}</td>
</tr>
</table>

运行


使用$even和$odd

<table>
<tr ng-repeat="x in names">
<td ng-if="$odd" style="background-color:#f1f1f1">{{ x.Name }}</td>
<td ng-if="$even">{{ x.Name }}</td>
<td ng-if="$odd" style="background-color:#f1f1f1">{{ x.Country }}</td>
<td ng-if="$even">{{ x.Country }}</td>
</tr>
</table>

运行

AngularJS快速入门指南08:表格的更多相关文章

  1. AngularJS快速入门指南09:SQL

    我们可以将之前章节中的代码用来从数据库中读取数据. 通过PHP Server从MySQL数据库中获取数据 <div ng-app="myApp" ng-controller= ...

  2. AngularJS快速入门指南07:Http对象

    $http是AngularJS提供的一个服务,用来从远程服务器读取数据. 提供数据 下面的数据由Web服务器提供: { "records": [ { "Name" ...

  3. AngularJS快速入门指南01:导言

    AngularJS使用新的attributes扩展了HTML AngularJS对单页面应用的支持非常好(SPAs) AngularJS非常容易学习 现在就开始学习AngularJS吧! 关于本指南 ...

  4. AngularJS快速入门指南19:示例代码

    本文给出的大部分示例都可以直接运行,通过点击运行按钮来查看结果,同时支持在线编辑代码. <div ng-app=""> <p>Name: <input ...

  5. AngularJS快速入门指南16:Bootstrap

    thead>tr>th, table.reference>tbody>tr>th, table.reference>tfoot>tr>th, table ...

  6. AngularJS快速入门指南20:快速参考

    thead>tr>th, table.reference>tbody>tr>th, table.reference>tfoot>tr>th, table ...

  7. AngularJS快速入门指南18:Application

    是时候创建一个真正的AngularJS单页面应用程序了(SPA). 一个AngularJS应用程序示例 你已经了解了足够多的内容来创建第一个AngularJS应用程序: My Note Save Cl ...

  8. AngularJS快速入门指南17:Includes

    使用AngularJS,你可以在HTML中包含其它的HTML文件. 在HTML中包含其它HTML文件? 当前的HTML文档还不支持该功能.不过W3C建议在后续的HTML版本中增加HTML import ...

  9. AngularJS快速入门指南15:API

    thead>tr>th, table.reference>tbody>tr>th, table.reference>tfoot>tr>th, table ...

随机推荐

  1. json转换为javabean

    public static void jSONObjectToJavaBean() throws ClassCastException{ JSONObject jsonObject = new JSO ...

  2. asp.net TreeView控件绑定数据库显示信息

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  3. <img>标签

    <img src="w3school.jpg" width="104" height="142" />注释:图像的名称和尺寸是以 ...

  4. [2015hdu多校联赛补题]hdu5297 Y sequence

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5297 题意:给你一个所有正整数的序列,然后去掉满足x^(2~r)的所有数(x为所有正整数,r>= ...

  5. Swift3.0基础语法学习<五>

    异常处理: // // ViewController5.swift // SwiftBasicDemo // // Created by 思 彭 on 16/11/16. // Copyright © ...

  6. 小甲鱼python视频弟十一讲(课后习题)

    1.修改列表里的值 list1 = [,,[,,,[,,,,] list1[] = print(list1) list1[][][] = '?' print(list1) 2.列表的排序(sort) ...

  7. 在windows环境下基于sublime text3的node.js开发环境搭建

    首先安装sublime text3,百度一堆,自己找吧.理论上sublime text2应该也可以.我只能说一句:这个软件实在是太强悍了. 跨平台,丰富的插件体系,加上插件基本上就是一个强悍的ide了 ...

  8. IIS访问提示您不具备查看该目录或页面的权限,因为访问控制列表 (ACL) 对 Web 服务器上的该资源进行了配置

    1. 检查IIS权限,webmail目录权限. 2. 在本地安全策略中,设置从网络访问本地服务器与拒绝从网络访问本地服务器.中查看相关用户的权限 3. 如果上面的步骤还是有问题.使用下面的步骤进行设置 ...

  9. Daily Scrum 12.3

    今日完成任务: 与安卓组进行商量对数据库修改的方案.现在在等他们最终确认,确认之后进行整理以及源代码的调试. 对资源功能的代码进行阅读. 遇到困难: 关于整合,爬虫组爬到的内容和网站定位有所不符,所以 ...

  10. iptables文件

    # Firewall configuration written by system-config-firewall# Manual customization of this file is not ...