thead>tr>th, table.reference>tbody>tr>th, table.reference>tfoot>tr>th, table.reference>thead>tr>td, table.reference>tbody>tr>td, table.reference>tfoot>tr>td {
padding: 8px;
line-height: 1.42857143;
vertical-align: top;
border-top: 1px solid #ddd;
}
div.chapter {
margin: 10px 0px 10px 0px;
padding: 0px;
width: auto;
overflow: hidden;
}
div.chapter div.prev {
width: 50%;
float: left;
text-align: left;
overflow: hidden;
white-space: nowrap;
}
div.chapter div.next {
width: 50%;
float: right;
text-align: right;
white-space: nowrap;
overflow: hidden;
}
-->

  Bootstrap是一套非常流行的样式表框架,本章用以演示如何在AngularJS中使用它。


Bootstrap

  为了在AngularJS application中使用Bootstrap,你需要将下面这行代码加入到页面的head部分(或者去Bootstrap官网下载包然后引用到页面上):

  1. <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">

  查看Bootstrap中文官网以了解更多有关http://www.bootcss.com/

  下面是一个完整的HTML示例,并附有AngularJS指令和Bootstrap类的说明。


HTML代码

  1. <!DOCTYPE html>
  2. <html>
  3. <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
  4. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
  5. <body ng-app="myApp" ng-controller="userCtrl">
  6.  
  7. <div class="container">
  8.  
  9. <h3>Users</h3>
  10.  
  11. <table class="table table-striped">
  12. <thead><tr>
  13. <th>Edit</th>
  14. <th>First Name</th>
  15. <th>Last Name</th>
  16. </tr></thead>
  17. <tbody><tr ng-repeat="user in users">
  18. <td>
  19. <button class="btn" ng-click="editUser(user.id)">
  20. <span class="glyphicon glyphicon-pencil"></span>&nbsp;&nbsp;Edit
  21. </button>
  22. </td>
  23. <td>{{ user.fName }}</td>
  24. <td>{{ user.lName }}</td>
  25. </tr></tbody>
  26. </table>
  27.  
  28. <hr>
  29. <button class="btn btn-success" ng-click="editUser('new')">
  30. <span class="glyphicon glyphicon-user"></span> Create New User
  31. </button>
  32. <hr>
  33.  
  34. <h3 ng-show="edit">Create New User:</h3>
  35. <h3 ng-hide="edit">Edit User:</h3>
  36.  
  37. <form class="form-horizontal">
  38. <div class="form-group">
  39. <label class="col-sm-2 control-label">First Name:</label>
  40. <div class="col-sm-10">
  41. <input type="text" ng-model="fName" ng-disabled="!edit" placeholder="First Name">
  42. </div>
  43. </div>
  44. <div class="form-group">
  45. <label class="col-sm-2 control-label">Last Name:</label>
  46. <div class="col-sm-10">
  47. <input type="text" ng-model="lName" ng-disabled="!edit" placeholder="Last Name">
  48. </div>
  49. </div>
  50. <div class="form-group">
  51. <label class="col-sm-2 control-label">Password:</label>
  52. <div class="col-sm-10">
  53. <input type="password" ng-model="passw1" placeholder="Password">
  54. </div>
  55. </div>
  56. <div class="form-group">
  57. <label class="col-sm-2 control-label">Repeat:</label>
  58. <div class="col-sm-10">
  59. <input type="password" ng-model="passw2" placeholder="Repeat Password">
  60. </div>
  61. </div>
  62. </form>
  63.  
  64. <hr>
  65. <button class="btn btn-success" ng-disabled="error || incomplete">
  66. <span class="glyphicon glyphicon-save"></span> Save Changes
  67. </button>
  68. </div>
  69.  
  70. <script src = "myUsers.js"></script>
  71. </body>
  72. </html>

运行


上例中出现的AngularJS指令解释

AngularJS指令 描述
<body ng-app 将<body>元素定义为AngularJS application的根元素。
<body ng-controller 在<body>元素中指定一个控制器。
<tr ng-repeat 对users对象的每一个子项都创建一个<tr>元素。
<button ng-click 当<button>元素被点击时,调用editUser()函数。
<h3 ng-show 当edit = true时显示<h3>元素。
<h3 ng-hide 当edit = true时隐藏<h3>元素。
<input ng-model 将<input>标签绑定到application。
<button ng-disabled 当出现错误或者incomplete = true时禁用<button>标签。

Bootstrap类解释

元素 Bootstrap类 定义
<div> container 定义内容的容器。
<table> table 定义一个表格。
<table> table-striped 定义一个带有striped样式的表格,即奇数行和偶数行的背景色不同。
<button> btn 定义一个按钮。
<button> btn-success 定义一个带有success样式的按钮。
<span> glyphicon 定义一个glyph样式的图标。
<span> glyphicon-pencil 定义一个pencil样式的图标。
<span> glyphicon-user 定义一个user样式的图标。
<span> glyphicon-save 定义一个save样式的图标。
<form> form-horizontal 定义一个horizontal样式的表单。
<div> form-group 定义一组控件。
<label> control-label 定义一个label控件。
<label> col-sm-2 定义一个具有两列的span。
<div> col-sm-10 定义一个具有10列的span。

JavaScript代码

  1. angular.module('myApp', []).controller('userCtrl', function($scope) {
  2. $scope.fName = '';
  3. $scope.lName = '';
  4. $scope.passw1 = '';
  5. $scope.passw2 = '';
  6. $scope.users = [
  7. {id:1, fName:'Hege', lName:"Pege" },
  8. {id:2, fName:'Kim', lName:"Pim" },
  9. {id:3, fName:'Sal', lName:"Smith" },
  10. {id:4, fName:'Jack', lName:"Jones" },
  11. {id:5, fName:'John', lName:"Doe" },
  12. {id:6, fName:'Peter', lName:"Pan" }
  13. ];
  14. $scope.edit = true;
  15. $scope.error = false;
  16. $scope.incomplete = false;
  17.  
  18. $scope.editUser = function(id) {
  19. if (id == 'new') {
  20. $scope.edit = true;
  21. $scope.incomplete = true;
  22. $scope.fName = '';
  23. $scope.lName = '';
  24. } else {
  25. $scope.edit = false;
  26. $scope.fName = $scope.users[id-1].fName;
  27. $scope.lName = $scope.users[id-1].lName;
  28. }
  29. };
  30.  
  31. $scope.$watch('passw1',function() {$scope.test();});
  32. $scope.$watch('passw2',function() {$scope.test();});
  33. $scope.$watch('fName', function() {$scope.test();});
  34. $scope.$watch('lName', function() {$scope.test();});
  35.  
  36. $scope.test = function() {
  37. if ($scope.passw1 !== $scope.passw2) {
  38. $scope.error = true;
  39. } else {
  40. $scope.error = false;
  41. }
  42. $scope.incomplete = false;
  43. if ($scope.edit && (!$scope.fName.length ||
  44. !$scope.lName.length ||
  45. !$scope.passw1.length || !$scope.passw2.length)) {
  46. $scope.incomplete = true;
  47. }
  48. };
  49.  
  50. });

JavaScript代码解释

Scope对象的属性 作用
$scope.fName 模型变量(用户first name)。
$scope.lName 模型变量(用户last name)。
$scope.passw1 模型变量(用户password 1)。
$scope.passw2 模型变量(用户password 2)。
$scope.users 模型变量(用户对象的数组)。
$scope.edit 当点击按钮创建一个用户时将该变量设置为true。
$scope.error 当passw1不等于passw2时值为true。
$scope.incomplete 当field中的内容为空时(即length = 0)值为true。
$scope.editUser 修改模型数据。
$scope.watch 监视模型数据(例如判断passw1是否等于passw2)。
$scope.test 进行数据验证,然后设置$scope.error和$scope.incomplete的值。

AngularJS快速入门指南16:Bootstrap的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. AngularJS快速入门指南14:数据验证

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

  8. AngularJS快速入门指南13:表单

    一个AngularJS表单是一组输入型控件的集合. HTML控件 HTML输入型标签标包括: input标签 select标签 button标签 textarea标签 HTML表单 HTML表单将各种 ...

  9. AngularJS快速入门指南12:模块

    AngularJS模块定义了一个application. 模块是一个application中不同部分的容器. application中的所有控制器都应该属于一个模块. 带有一个控制器的模块 下面这个a ...

随机推荐

  1. Android手机编程初学遇到的问题及解决方法

    对高手来讲不值一提,可是对我这个初学来讲却是因为这些问题费了老长时间,有的不是编程问题,但不注意也会浪费不少宝贵时间!随时遇到随时更新... 引入第三方类库的问题,开始引用后没什么问题,但发现了该类库 ...

  2. 1245 - Harmonic Number (II)---LightOJ1245

    http://lightoj.com/volume_showproblem.php?problem=1245 题目大意:一个数n除以1到n之和 分析:暴力肯定不行,我们可以先求1~sqrt(n)之间的 ...

  3. MVC文件上传和下载

    1.单个文件上传 HTML写法:form表单中加enctype="multipart/form-data" <form aciont="" method= ...

  4. Unity加载模块深度解析(网格篇)

    在上一篇 加载模块深度解析(一)中,我们重点讨论了纹理资源的加载性能.这次,我们再来为你揭开其他主流资源的加载效率. 这是侑虎科技第53篇原创文章,欢迎转发分享,未经作者授权请勿转载.同时如果您有任何 ...

  5. MAT-Java内存分析工具

    对Mat工具的详细介绍,引用博文:http://my.oschina.net/biezhi/blog/286223 下载地址:http://www.eclipse.org/mat/downloads. ...

  6. Python 3 —— 控制语句

    控制语句 1.if if <s>: ... elif <s>: ... else: ... 2 for for e in list .. if <s> break; ...

  7. Tomcat实现分析(一)--类加载及容器组件

    启动脚本 启动命令中的参数如下: org.apache.catalina.startup.Bootstrap start -Djava.util.logging.config.file=/opt/ap ...

  8. winRT Com组件开发流程总结

    winRT Com组件开发: 1.编辑idl文件,winRT COM的idl文件与win32的idl文件有差异,如下: interface ItestWinRTClass; runtimeclass ...

  9. AutoVue ISDK学习总结(一)

    这段时间,断断续续花了2,3周时间,研究AutoVue ISDK.貌似这个东西用的人很少,网上只搜到了一篇中文文档.自己英文水平差,Java水平也差,看Oracle官网文档,进展非常慢.到现在,终于对 ...

  10. alfresco install in linux, and integrated with tesseract ocr

    本文描述在Linux系统上安装Alfresco的步骤: 1. 下载安装文件:alfresco-community-5.0.d-installer-linux-x64.bin 2. 增加执行权限并执行: ...