angular表单验证实例讲解
定义验证规则,验证有效性 显示验证结果 禁用html5自带验证,novalidate=novalidate”“ 用户输入后,angular会依次调用验证器进行验证,全部成功时model会变成用户输入的值 不成功则保留原值,并在model上增加一个$error对象
变量名 | 含义 |
---|---|
$dirty | 表单中任何一项是否输入过 |
$pristine | 表单中任何一项尚未输入过 |
$error | 存放错误信息 |
$invalid | 表单数据是否无效,只要有一项无效则整个表单无效 |
$valid | 和上面相反 |
$name | 表单的名字 |
表单各个输入框的model |
form错误提示
<!doctype html>
<html ng-app="zz">
<head>
<meta charset="utf-8">
<title></title>
<link href="js/bootstrap.min.css" rel="stylesheet"/>
</head>
<body ng-cpntroller="formctrl">
<p class="container">
<form name="userform" novalidata="novalidata">
<p class="form-group" ng-class="{'has-success':userform.email.$valid&&userform.email.$dirty,'has-error':userform.email.$invalid&&userform.email.$dirty}">
<label for="email">邮箱</label>
<input type="email" ng-minlength="3" name="email"id="email" class="form-control" ng-model="email" ng-required="true" />
<p ng-show="userform.email.$error.required&&userform.email.$dirty">此字段必输</p>
<p ng-show="userform.email.$error.email&&userform.email.$dirty">请输入正确的邮箱</p>
<p ng-show="userform.email.$error.minlength&&userform.email.$dirty">不能少于3位</p>
</p>
<p class="form-group">
<input type="button" value="提交" class="btn btn-default" />
</p>
</form>
</p>
<pre>
{{userform|json}}
</pre>
</body>
<script src="js/angular.js"></script>
<script type="text/javascript">
angular.module('zz',[]).controller('formctrl',function(){
})
</script>
</html>
编译一个模板
用$compile.他是angular提供的一个服务
form自定义提示
<!doctype html>
<html ng-app="zz">
<head>
<meta charset="utf-8">
<title></title>
<link href="js/bootstrap.min.css" rel="stylesheet"/>
</head>
<body ng-cpntroller="formctrl">
<p class="container">
<form name="userform" novalidata="novalidata">
<p class="form-group" ng-class="{'has-success':userform.email.$valid&&userform.email.$dirty,'has-error':userform.email.$invalid&&userform.email.$dirty}">
<label for="email">邮箱</label>
<input zz-errors type="email" ng-minlength="3" name="email"id="email" class="form-control" ng-model="email" ng-required="true" />
<!--<p class="help-block" ng-show="userform.email.$error.required&&userform.email.$dirty">此字段必输</p>
<p ng-show="userform.email.$error.email&&userform.email.$dirty">请输入正确的邮箱</p>
<p ng-show="userform.email.$error.minlength&&userform.email.$dirty">不能少于3位</p>-->
</p>
<p class="form-group">
<input type="button" value="提交" class="btn btn-default" />
</p>
</form>
</p>
<pre>
{{userform|json}}
</pre>
</body>
<script src="js/angular.js"></script>
<script type="text/javascript">
angular.module('zz',[]).controller('formctrl',function(){
}).directive('zzerrors',function($compile){
return{
require:'ngmodel',
link:function(scope,element,attrs,ngmodelctrl){
var subscope=scope.$new(true);//独立的scope
subscope.haserrors=function(){
return ngmodelctrl.$invalid;
}
subscope.errors=function(){
return ngmodelctrl.$error;
}
var templ='<p class="help-block" ng-show="haserrors()" ng-repeat="(key,value) in errors()">{{key}}</p>'
var message=$compile(templ)(subscope);
element.after(message);
}
}
})
</script>
</html>
远程验证
<!doctype html>
<html ng-app="zz">
<head>
<meta charset="utf-8">
<title></title>
<link href="js/bootstrap.min.css" rel="stylesheet"/>
</head>
<body ng-cpntroller="formctrl">
<p class="container">
<form name="userform" novalidata="novalidata">
<p class="form-group" ng-class="{'has-success':userform.email.$valid&&userform.email.$dirty,'has-error':userform.email.$invalid&&userform.email.$dirty}">
<label for="email">邮箱</label>
<input zz-errors type="email" ng-minlength="3" name="email"id="email" class="form-control" ng-model="email" ng-required="true" />
</p>
<p class="form-group" ng-class="{'has-success':userform.username.$valid&&userform.username.$dirty,'has-error':userform.username.$invalid&&userform.username.$dirty}">
<label for="username">用户名</label>
<input zz-errors zz-unique type="username" ng-maxlength="12" name="username"id="username" class="form-control" ng-model="username" ng-required="true" />
</p>
<p class="form-group">
<input type="button" value="提交" class="btn btn-default" />
</p>
</form>
</p>
<pre>
{{userform|json}}
</pre>
</body>
<script src="js/angular.js"></script>
<script type="text/javascript">
angular.module('zz',[]).controller('formctrl',function(){
}).directive('zzerrors',function($compile){
return{
require:'ngmodel',
link:function(scope,element,attrs,ngmodelctrl){
var subscope=scope.$new(true);//独立的scope
subscope.haserrors=function(){
return ngmodelctrl.$invalid;
}
subscope.errors=function(){
return ngmodelctrl.$error;
}
var templ='<p class="help-block" ng-show="haserrors()" ng-repeat="(key,value) in errors()">{{key|errors}}</p>'
var message=$compile(templ)(subscope);
element.after(message);
}
}
}).filter('errors',function(){
return function(input){
var messager={
'required':'必填',
'email':'必须是邮箱',
'unqiue':'已经被占用了'
}
return messager[input];
}
}).directive('zzunique',function($http){
//1,监听数据变化
//2,当数据变化时到后台验证
//3,用返回值设置字段的有效性
return {
require:'ngmodel',
link:function(scope,element,attrs,ngmodelctrl){
scope.$watch(attrs.ngmodel,function(newvalue,oldvalue){
//scope[attrs.ngmodel]/ngmodelctrl.modelvalue
console.log(newvalue);
$http({
method:'post',
url:'https://localhost:3000/check',
data:{username:newvalue},
//headers:{'content-type':'application/json'}
}).then(function(result){
var data=result.data;
ngmodelctrl.$setvalidity('unique',data.unique)
})
});
}
}
})
</script>
</html>
服务https://localhost:3000/check的代码
var express=require('express');
var app=express();
var bodyparser=require('body-parser');
app.use(bodyparser.json());
app.post('/check',function(req,res){
res.setheader('access-control-allow-origin','*');
res.setheader('access-control-allow-headers','content-type');
var data=req.body;
if(data.username=='admin'){
res.send({unique:false});
}else{
res.send({unique:true});
}
});
app.all('/check',function(req,res){
res.setheader('access-control-allow-origin','*');
res.setheader('access-control-allow-headers','content-type');
res.send({});
});
app.listen(3000);