定义验证规则,验证有效性 显示验证结果 禁用html5自带验证,novalidate=novalidate”“ 用户输入后,angular会依次调用验证器进行验证,全部成功时model会变成用户输入的值 不成功则保留原值,并在model上增加一个$error对象
form错误提示
<!doctype html>
<html ng-app=”zz”>
<head>
<meta chart=”utf-8″>
<title></title>
<link href=”js/bootstrap.min.css” rel=”stylesheet”/>
</head>
<body ng-cpntroller=”formctrl”>
<p class=”container”>
<form name=”urform” novalidata=”novalidata”>
<p class=”form-group” ng-class=”{‘has-success’:urform.email.$valid&&urform.email.$dirty,’has-error’:urform.email.$invalid&&urform.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=”urform.email.$error.required&&urform.email.$dirty”>此字段必输</p>
<p ng-show=”urform.email.$error.email&&urform.email.$dirty”>请输入正确的邮箱</p>
<p ng-show=”urform.email.$error.minlength&&urform.email.$dirty”>不能少于3位</p>
</p>
<p class=”form-group”>
<input type=”button” value=”提交” class=”btn btn-default” />
</p>
</form>
</p>
<pre>
{{urform|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 chart=”utf-8″>
<title></title>
<link href=”js/bootstrap.min.css” rel=”stylesheet”/>
</head>
<body ng-cpntroller=”formctrl”>
<p class=”container”>
<form name=”urform” novalidata=”novalidataR师说注音21;>
<p class=”form-group” ng-class=”{‘has-success’:urform.email.$valid&&urform.email.$dirty,’has-error’:urform.email.$invalid&&urform.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=”urform.email.$error.required&&urform.email.$dirty”>此字段必输</p>
<p ng-show=”urform.email.$error.email&&urform.email.$dirty”>请输入正确的邮箱</p>入伏从哪一天开始算起
<p ng-show=”urform.email.$error.minlength&&urform.email.$dirty”>不能少于3位</p>–>
</p>
<p class=”form-group”>
<input type=”button” value=”提交” class=”btn btn-default” />
</p>
</form>
</p>
<pre>
{{urform|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.harrors=function(){
return ngmodelctrl.$invalid;
}
subscope.errors=function(){
五年级上册语文课文 return ngmodelctrl.$error;
}
var templ='<p class=”help-block” ng-show=”harrors()” 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 chart=”utf-8″>
<title></title>
<link href=”js/bootstrap.min.css” rel=”stylesheet”/>
</head>
<body ng-cpntroller=”formctrl”>
<p class=”container”>
<form name=”urform” novalidata=”novalidata”>
<p class=”form-group” ng-class=”{‘has-success’:urform.email.$valid&&urform.email.$dirty,’has-error’:urform.email.$invalid&&urform.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’:urform.urname.$valid&&urform.urname.$dirty,’has-error’:urform.urname.$invalid&&urform.urname.$dirty}”>
<label for=”urname”>用户名</label>
<input zz-errors zz-unique type=”urname” ng-maxlength=”12″ name=”urname”id=”urname” class=”form-control” ng-model=”urname” ng-required=”true” />
</p>
<p class=”form-group”>
<input type=”button” value=”提交” class=”btn btn-default” />
</p>
</form>
</p>
<pre>
{{urform|json}}
</pre>
</body>
<script src=”js/angular.js”></script>
<script type=”text/javascr新民主主义革命时期ipt”>
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.harrors=function(){
return ngmodelctrl.$invalid;
}
subscope.errors=function(){
return ngmodelctrl.$error;
}
var templ='<p class=”help-block” ng-show=”harrors()” 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:{urname:newvalue},
//headers:{‘content-type’:’application/json’}
}).then(function(result){
var data=result.data;
ngmodelctrl.$tvalidity(‘unique’,data.unique)
})
});
}
}
})
</script>
</html>
服务https://localhost:3000/check的代码
var express=require(‘express’);
var app=express();
var bodyparr=require(‘body-parr’);
app.u(bodyparr.json());
app.post(‘/check’,function(req,res){
res.theader(‘access-control-allow-origin’,’*’);
res.theader(‘access-control-allow-headers’,’content-type’);
var data=req.body;
if(data.urname==’admin’){
res.nd({unique:fal});
}el{
res.nd({unique:true});
}
});
app.all(‘/check’,function(req,res){
res.theader(‘access-control-allow-origin’,’*’);
res.theader(‘access-control-allow-headers’,’content-type’);
res.nd({});
});
app.listen(3000);
本文发布于:2023-04-03 15:19:58,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/7b838eeb893bf7ad23fc3857963fa85a.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:angular表单验证实例讲解.doc
本文 PDF 下载地址:angular表单验证实例讲解.pdf
留言与评论(共有 0 条评论) |