AngularJS 与 W3.CSS
你可以很容易地使用 w3.css 样式表和 AngularJS。本章将演示如何来使用。
W3.CSS
要在 AngularJS 应用程序包含 W3.CSS ,在文档的开头添加以下行:
<link rel="stylesheet" href="https://cankaoshouce.com/css/w3.css">
如果您想学习 W3.CSS, 请访问本站的 W3.CSS 教程.
下面是一个完整的 HTML 示例,包含所有 AngularJS 指令和 W3.CSS 类:
HTML 代码
<!DOCTYPE html>
<html>
<link rel="stylesheet" href="https://cankaoshouce.com/css/w3.css">
<script src="https://cdn.bootcdn.net/ajax/libs/angular.js/1.8.2/angular.min.js"></script>
<body ng-app="myApp" ng-controller="userCtrl">
<div class="w3-container">
<h3>用户</h3>
<table class="w3-table w3-bordered w3-striped">
<tr>
<th>编辑</th>
<th>姓氏</th>
<th>名字</th>
</tr>
<tr ng-repeat="user in users">
<td>
<button class="w3-btn w3-ripple" ng-click="editUser(user.id)">✎ Edit</button>
</td>
<td>{{ user.fName }}</td>
<td>{{ user.lName }}</td>
</tr>
</table>
<br>
<button class="w3-btn w3-green w3-ripple" ng-click="editUser('new')">✎ 创建新用户</button>
<form ng-hide="hideform">
<h3 ng-show="edit">创建新用户:</h3>
<h3 ng-hide="edit">修改用户:</h3>
<label>姓氏:</label>
<input class="w3-input w3-border" type="text" ng-model="fName" ng-disabled="!edit" placeholder="First Name">
<br>
<label>名字:</label>
<input class="w3-input w3-border" type="text" ng-model="lName" ng-disabled="!edit" placeholder="Last Name">
<br>
<label>密码:</label>
<input class="w3-input w3-border" type="password" ng-model="passw1" placeholder="Password">
<br>
<label>Repeat:</label>
<input class="w3-input w3-border" type="password" ng-model="passw2" placeholder="Repeat Password">
<br>
<button class="w3-btn w3-green w3-ripple" ng-disabled="error || incomplete">✔ 保存修改</button>
</form>
</div>
<script src= "/znadmin/md/3858/myUsers.js"></script>
</body>
</html>
解释 (上面的) 指令
AngularJS 指令 | 描述 |
---|---|
<body ng-app | 定义 <body> 元素的应用程序 |
<body ng-controller | 定义一个 <body> 元素的控制器 |
<tr ng-repeat | 为用户中的每个用户重复 <tr> 元素 |
<button ng-click | 当 <button> 元素被点击,就调用 editUser() 函数 |
<h3 ng-show | 如果 edit = true,展示 <h3> 元素 |
<h3 ng-hide | Hide the form if hideform = true, and hide the <h3> element if edit = true |
<input ng-model | Bind the <input> element to the application |
<button ng-disabled | Disables the <button> element if error or incomplete = true |
W3.CSS 样式表解释
元素 | 样式 | 定义 |
---|---|---|
<div> | w3-container | 一个内容容器 |
<table> | w3-table | 一个表格 |
<table> | w3-bordered | 一个表格边框 |
<table> | w3-striped | 有边框的表格 |
<button> | w3-btn | 一个按钮 |
<button> | w3-green | 一个绿色按钮 |
<button> | w3-ripple | 当你点击按钮时会产生连锁反应 |
<input> | w3-input | 一个输入框 |
<input> | w3-border | 输入字段上的边框 |
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.hideform = true;
$scope.editUser = function(id) {
$scope.hideform = false;
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 | 模型变量(用户前名) |
$scope.lName | 模型变量(用户后名) |
$scope.passw1 | 模型变量(用户密码 1) |
$scope.passw2 | 模型变量(用户密码 2) |
$scope.users | 模型变量(用户数组) |
$scope.edit | 当用户单击 'Create user' 时设置为 true。 |
$scope.hideform | 当用户单击 'Edit' 或 'Create user' 时,设置为 false |
$scope.error | 如果 passw1 不等于 passw2,设置为 true |
$scope.incomplete | 如果字段是空的 (length = 0),设置为 true |
$scope.editUser | 设置模型变量 |
$scope.watch | 观察模型变量 |
$scope.test | 测试模型变量的错误和不完整性 |