1. 理論知識
- $invalid: 一旦表單沒有驗證通過,AngularJS就會為表單設(shè)置為$invalid屬性
- 驗證器(用來驗證表單)
1. ng-required
2. ng-minlength
3. type="email"
4. type="url"
5. type="date"
6. 等等
- 驗證后的結(jié)果狀態(tài)(boolean類型)窟感。一般是$error.email或者$error.minlength
1. $invalid
2. email
3. minlength
4. 等等
- 驗證后的結(jié)果狀態(tài) 對應(yīng)的 css class
1. $invalid--> ng-invalid
2. email --> ng-valid-email / ng-invalid-email
3. minlength --> ng-valid-minlength / ng-invalid-minlength
2. error_msg.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>錯誤消息</title>
<meta name="keywords" content="錯誤消息" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0" name="viewport">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<script src="js/common/angular.min.js"></script>
<script src="js/common/angular-ui-router.min.js"></script>
<style>
.username.ng-dirty.ng-invalid-minlength, .email.ng-dirty.ng-invalid-email{
background-color: lightpink;
}
.username.ng-valid, .email.ng-valid{
background-color: green;
}
</style>
</head>
<body ng-app="notesApp" ng-controller="MainCtrl as ctrl">
<div>
<form ng-submit="ctrl.submit()" name="myform">
username: <input type="text"
class="username"
name="uname"
ng-model="ctrl.username"
ng-minlength="4">
<span ng-show="myform.uname.$error.minlength">
最小長度為4</span>
<br>
email: <input class="email"
name="email"
ng-model="ctrl.email"
type="email">
<span ng-show="myform.email.$error.email">
郵箱格式出錯</span>
<br>
password: <input type="password"
ng-model="ctrl.password">
<br>
<button ng-click="ctrl.change()">重置為初始數(shù)據(jù)</button>
<input type="submit"
value="submit"
ng-disabled="myform.$invalid">
</form>
</div>
</body>
<script type="text/javascript">
angular.module('notesApp',[])
.controller('MainCtrl', [function(){
var self = this;
self.username = 'tinygao';
self.email = 'tinygao@163.com';
self.password = 'helloworld';
console.log('Main Ctrl');
self.change = function() {
self.username = 'tinygao';
self.email = 'tinygao@163.com';
self.password = 'helloworld';
};
self.submit = function() {
console.log('user click submit with ', self.username, self.password);
};
}]);
</script>
</html>
3. 展示頁面
4. 解析
- 表單校驗器: ng-minlength="4"
- 校驗狀態(tài)結(jié)果: myform.uname.$error.minlength
- css對應(yīng)狀態(tài)結(jié)果表示:
- .username.ng-dirty.ng-invalid-minlength
- .username.ng-valid
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者