AngularJS快速入门指南16:Bootstrap
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官网下载包然后引用到页面上):
<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代码
<!DOCTYPE html>
<html>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body ng-app="myApp" ng-controller="userCtrl"> <div class="container"> <h3>Users</h3> <table class="table table-striped">
<thead><tr>
<th>Edit</th>
<th>First Name</th>
<th>Last Name</th>
</tr></thead>
<tbody><tr ng-repeat="user in users">
<td>
<button class="btn" ng-click="editUser(user.id)">
<span class="glyphicon glyphicon-pencil"></span> Edit
</button>
</td>
<td>{{ user.fName }}</td>
<td>{{ user.lName }}</td>
</tr></tbody>
</table> <hr>
<button class="btn btn-success" ng-click="editUser('new')">
<span class="glyphicon glyphicon-user"></span> Create New User
</button>
<hr> <h3 ng-show="edit">Create New User:</h3>
<h3 ng-hide="edit">Edit User:</h3> <form class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label">First Name:</label>
<div class="col-sm-10">
<input type="text" ng-model="fName" ng-disabled="!edit" placeholder="First Name">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Last Name:</label>
<div class="col-sm-10">
<input type="text" ng-model="lName" ng-disabled="!edit" placeholder="Last Name">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Password:</label>
<div class="col-sm-10">
<input type="password" ng-model="passw1" placeholder="Password">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Repeat:</label>
<div class="col-sm-10">
<input type="password" ng-model="passw2" placeholder="Repeat Password">
</div>
</div>
</form> <hr>
<button class="btn btn-success" ng-disabled="error || incomplete">
<span class="glyphicon glyphicon-save"></span> Save Changes
</button>
</div> <script src = "myUsers.js"></script>
</body>
</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代码
angular.module('myApp', []).controller('userCtrl', function($scope) {
$scope.fName = '';
$scope.lName = '';
$scope.passw1 = '';
$scope.passw2 = '';
$scope.users = [
{id:1, fName:'Hege', lName:"Pege" },
{id:2, fName:'Kim', lName:"Pim" },
{id:3, fName:'Sal', lName:"Smith" },
{id:4, fName:'Jack', lName:"Jones" },
{id:5, fName:'John', lName:"Doe" },
{id:6, fName:'Peter', lName:"Pan" }
];
$scope.edit = true;
$scope.error = false;
$scope.incomplete = false;
$scope.editUser = function(id) {
if (id == 'new') {
$scope.edit = true;
$scope.incomplete = true;
$scope.fName = '';
$scope.lName = '';
} else {
$scope.edit = false;
$scope.fName = $scope.users[id-1].fName;
$scope.lName = $scope.users[id-1].lName;
}
};
$scope.$watch('passw1',function() {$scope.test();});
$scope.$watch('passw2',function() {$scope.test();});
$scope.$watch('fName', function() {$scope.test();});
$scope.$watch('lName', function() {$scope.test();});
$scope.test = function() {
if ($scope.passw1 !== $scope.passw2) {
$scope.error = true;
} else {
$scope.error = false;
}
$scope.incomplete = false;
if ($scope.edit && (!$scope.fName.length ||
!$scope.lName.length ||
!$scope.passw1.length || !$scope.passw2.length)) {
$scope.incomplete = true;
}
};
});
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的更多相关文章
- AngularJS快速入门指南17:Includes
使用AngularJS,你可以在HTML中包含其它的HTML文件. 在HTML中包含其它HTML文件? 当前的HTML文档还不支持该功能.不过W3C建议在后续的HTML版本中增加HTML import ...
- AngularJS快速入门指南15:API
thead>tr>th, table.reference>tbody>tr>th, table.reference>tfoot>tr>th, table ...
- AngularJS快速入门指南01:导言
AngularJS使用新的attributes扩展了HTML AngularJS对单页面应用的支持非常好(SPAs) AngularJS非常容易学习 现在就开始学习AngularJS吧! 关于本指南 ...
- AngularJS快速入门指南20:快速参考
thead>tr>th, table.reference>tbody>tr>th, table.reference>tfoot>tr>th, table ...
- AngularJS快速入门指南19:示例代码
本文给出的大部分示例都可以直接运行,通过点击运行按钮来查看结果,同时支持在线编辑代码. <div ng-app=""> <p>Name: <input ...
- AngularJS快速入门指南18:Application
是时候创建一个真正的AngularJS单页面应用程序了(SPA). 一个AngularJS应用程序示例 你已经了解了足够多的内容来创建第一个AngularJS应用程序: My Note Save Cl ...
- AngularJS快速入门指南14:数据验证
thead>tr>th, table.reference>tbody>tr>th, table.reference>tfoot>tr>th, table ...
- AngularJS快速入门指南13:表单
一个AngularJS表单是一组输入型控件的集合. HTML控件 HTML输入型标签标包括: input标签 select标签 button标签 textarea标签 HTML表单 HTML表单将各种 ...
- AngularJS快速入门指南12:模块
AngularJS模块定义了一个application. 模块是一个application中不同部分的容器. application中的所有控制器都应该属于一个模块. 带有一个控制器的模块 下面这个a ...
随机推荐
- prism4 StockTrader RI 项目分析一些体会2
prism 对于逻辑复杂的页面,通过建立 controller实现逻辑管理 按着一般的做法就是,各模块的viewmodel import由各模块去实例化(理解有限),但是通过controller实现了 ...
- 需要了解的 Linux 网络和监控命令
列出来的10个基础的每个linux用户都应该知道的网络和监控命令.网络和监控命令类似于这些: hostname, ping, ifconfig, iwconfig, netstat, nslookup ...
- css3中的字体样式
text-overform:ellipsis省略号/clip裁剪. overform:hidden溢出隐藏文字. 但是text-overflow只是用来说明文字溢出时用什么方式显示,要实现溢出时产生省 ...
- mac系统 下 npm 安装 bower报错
在mac终端运行 sudo npm install -g bower (安装之前你要确定你已经成功安装了node 和 git) 然后会报错 like this: npm ERR! Darwin 15. ...
- 原生态jdbc的应用技术
为了更好的了解jdbc,最近查阅了前期学习的资料,整理归纳了一下,整理出来了一套jdbc常用的工具类.之所以在这里撰文,一来可以和大家共享技术的魅力,二来可以方便以后的查阅方便.以下是一个jdbc的优 ...
- 总有一项适合你:联想 Miix2 8寸版触摸屏失灵的各项解决方案
今天试着自己拆开后盖重新拆了一下排线,果然这个方法才是王道.在搜索攻略的时候看到了下面的帖子,觉得总结的不错,特此转载过来: 白色石头 2015-05-22 10:07● 使用评测 总有一 ...
- 如何编写高效的jQuery代码
jQuery的编写原则: 一.不要过度使用jQuery 1. jQuery速度再快,也无法与原生的javascript方法相比,而且建立的jQuery对象包含的信息量很庞大.所以有原生方法可以使用的场 ...
- 异或链表(XOR linked list)
异或链表(Xor Linked List)也是一种链式存储结构,它可以降低空间复杂度达到和双向链表一样目的,任何一个节点可以方便的访问它的前驱节点和后继结点.可以参阅wiki 普通的双向链表 clas ...
- flash 定义二维数组
一种二维数组的定义方法 //假设二维数组为 [5][7]var xn:Number = 5;var yn:Number = 7; //定义一数值变量var temp:Number = 0; ...
- mysql用户权限设置
1.创建新用户 通过root用户登录之后创建 >> grant all privileges on *.* to testuser@localhost identified by &quo ...