required placeholder="请输入密码">
js
.directive("equalTo",function () {
return {
require: "ngModel",link: function (scope,ele,attrs,ctrl) {
原文链接:https://www.f2er.com/js/44813.htmlreturn {
require: "ngModel",link: function (scope,ele,attrs,ctrl) {
console.log(scope);//打印当前作用域
console.log(attrs);//打印当前<a href="https://www.jb51.cc/tag/biaoqian/" target="_blank" class="keywords">标签</a><a href="https://www.jb51.cc/tag/shuxing/" target="_blank" class="keywords">属性</a>列表
console.log(ctrl);//打印当前ctrl
var target = attrs["equalTo"];//<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="https://www.jb51.cc/tag/zidingyi/" target="_blank" class="keywords">自定义</a>指令<a href="https://www.jb51.cc/tag/shuxing/" target="_blank" class="keywords">属性</a>键值
if (target) {//判断键是否存在
scope.$watch(target,function () {//存在启动监听其值
ctrl.$validate()//每次改变手动<a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" class="keywords">调用</a>验证
})
// <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>当前模型控制器的父控制器FormController
var targetCtrl = ctrl.$$parentForm[target];//<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>指定模型控制器
console.log(targetCtrl)
ctrl.$validators.equalTo = function (modelValue,viewVale) {//<a href="https://www.jb51.cc/tag/zidingyi/" target="_blank" class="keywords">自定义</a>验证器<a href="https://www.jb51.cc/tag/neirong/" target="_blank" class="keywords">内容</a>
var targetValue = targetCtrl.$viewValue;//<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>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;
})
}
}
}
})