angularjs 表单密码验证自定义指令实现代码
html代码
<form name="form"> <input type="password" name="password" ng-model="password" required placeholder="请输入密码"> <input type="password" name="passwordConfirm" ng-model="passwordConfirm" equal-to="password" placeholder="请再次输入密码"> <span ng-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 var target = attrs["equalTo"];//获取自定义指令属性键值 if (target) {//判断键是否存在 scope.$watch(target, function () {//存在启动监听其值 ctrl.$validate()//每次改变手动调用验证 }) // 获取当前模型控制器的父控制器FormController var targetCtrl = ctrl.$$parentForm[target];//获取指定模型控制器 console.log(targetCtrl) ctrl.$validators.equalTo = function (modelValue, viewVale) {//自定义验证器内容 var targetValue = targetCtrl.$viewValue;//获取password的输入值 return targetValue == viewVale;//是否等于passwordConfirm的值 } ctrl.$formatters.push(function (value) { console.log("正在进行数据格式化的值:",value) return value; }) ctrl.$parsers.push(function (value) { console.log("正在进行数据转换的值:",value) return value; }) } } } })
相关推荐
youyouzouzou 2014-05-30
SakuraLu 2017-05-11
CrazyDogWang 2016-08-31
CrazyDogWang 2016-04-12
zhenghao 2014-05-30
SakuraLu 2019-06-26
princejingyun 2017-07-30
雅特岚蒂斯 2018-08-24
youyouzouzou 2015-08-24
SakuraLu 2016-11-25
GoDotDotDot 2018-12-12
samllcat 2019-06-29
youyouzouzou 2016-12-01
princejingyun 2016-11-25
youyouzouzou 2016-05-18
zbwroom 2015-12-11
princejingyun 2015-08-24
zbwroom 2014-11-24
kkkder 2019-06-21