angularjs 表单密码验证自定义指令实现代码
html代码
<formname="form"> <inputtype="password"name="password"ng-model="password"requiredplaceholder="请输入密码"> <inputtype="password"name="passwordConfirm"ng-model="passwordConfirm"equal-to="password"placeholder="请再次输入密码"> <spanng-show="form.passwordConfirm.$error.equalTo">两次密码不一致</span> </form>
js
angular.module("Valid",[])
.directive("equalTo",function(){
return{
require:"ngModel",
link:function(scope,ele,attrs,ctrl){
console.log(scope);//打印当前作用域
console.log(attrs);//打印当前标签属性列表
console.log(ctrl);//打印当前ctrl
vartarget=attrs["equalTo"];//获取自定义指令属性键值
if(target){//判断键是否存在
scope.$watch(target,function(){//存在启动监听其值
ctrl.$validate()//每次改变手动调用验证
})
//获取当前模型控制器的父控制器FormController
vartargetCtrl=ctrl.$$parentForm[target];//获取指定模型控制器
console.log(targetCtrl)
ctrl.$validators.equalTo=function(modelValue,viewVale){//自定义验证器内容
vartargetValue=targetCtrl.$viewValue;//获取password的输入值
returntargetValue==viewVale;//是否等于passwordConfirm的值
}
ctrl.$formatters.push(function(value){
console.log("正在进行数据格式化的值:",value)
returnvalue;
})
ctrl.$parsers.push(function(value){
console.log("正在进行数据转换的值:",value)
returnvalue;
})
}
}
}
})
演示地址:https://tianyouh.github.io/angularPasswordConfirm/