Angular JS 学习之Bootstrap
1.要使用Bootstrap框架,必须在<head>中加入链接:
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
2.国内站点建议使用:
<link rel="stylesheet" href="//apps.bdimg.com/libs/bootstrap/3.3.4/css/bootstrap.min.css">
3.实例演示:
<!DOCTYPE html>
<html>
<link rel="stylesheet" href="http://apps.bding.com/libs/bootstrap/3.3.4/css/bootstrap/.min.js">
<script src=http://apps.bding.com/libs/angular.js/1.4.6/angular.min.js></script>
<body ng-app="myApp" ng-controller="userCtrl"> //为<body>元素定义一个控制器
<div class="container"> //内容容器
<h3>User</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"> // 循环users对象组,每个user对象放在<tr>元素中
<td>
<button class="btn" ng-click="editUser(user.id)"> //当点击<button>元素时调用函数editUser()
<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>Ctreate New User //用户图标
</button>
<hr>
<h3 ng-show="edit">Create New User:</h3> //如果edit=“true”显示<h3>元素
<h3 ng-hide="edit">Edit User:</h3> //如果edit="true"隐藏<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"> //为应用程序绑定<input>元素
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Last Name:</label> //跨越2列
<div class="col-sm-10"> //跨越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"> //如果发生错误或者ncomplete=true禁用<button>元素
<span class="glyphicon glyphicon-save"></span>Save Changes
</button>
</div>
<script src="myUsers.js"></script>
</body>
</html>
4.Javascript代码:myUsers.js
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;
}
};
});