您的位置:首页 > Web前端 > AngularJS

[乐意黎转载]AngularJS快速入门指南16:Bootstrap

2017-03-02 15:14 701 查看
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的值。
本文转载自: http://www.cnblogs.com/jaxu/p/4504923.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: