判断两个密码是否输入一直
app.directive('myPwdMatch', [function(){ return { restrict: "A", //当require:'ngModel',时,link的第四个参数是 表单对象; require: 'ngModel', link: function(scope,element,attrs,ctrl){ console.info("scope",scope); console.info("ctrl",ctrl); var tageCtrl = scope.$eval(attrs.myPwdMatch); //var aaa = attrs.myPwdMatch // 返回值是scope[aaa],因为一般情况下aaa = 'myForm.cpwd',所以如果要用[]来获取的话应该: /* var arr1 = aaa.split("."); function parse(scope,arr){ for(var i = 0; i < arr.length; i++){ var str = arr[i] var temp = scope[str] return parse(temp); } } parse(scope,arr1); 这样可以获取和var tageCtrl = scope.$eval(attrs.myPwdMatch)相同的结果 */ // ; scope.$eval(str),是获取scope上的属性str,对应的值; //用scope:{myPwdMatch:"="},双向数据绑定能达到相同的结果 console.info("name",tageCtrl); //$parsers数组里面的函数都会被执行,参数为ng-model重读取到的值;数组会依次执行里面的函数 //把上一个函数的返回值,作为下一个函数的参数,(管道模式),直到最后一个函数的返回值会赋值到ng-model //$formatters也是一个类似的数组主要用于格式化和转换,以便显示 //$viewChangeListeners也是数组,当视图值改变时,函数回调用,没有参数,返回值会被忽略,用来执行与ng-model无关的其他函数; tageCtrl.$parsers.push(function(viewValue){ //$setValidity(错误的名称,bool)设置错误的标志, ctrl.$setValidity('pwdmatch', viewValue == ctrl.$viewValue); return viewValue; }); ctrl.$parsers.push(function(viewValue){ if(viewValue == tageCtrl.$viewValue){ ctrl.$setValidity('pwdmatch', true); return viewValue; } else{ ctrl.$setValidity('pwdmatch', false); return undefined; } }); } }; }]); //显示提示内容的指令 app.directive("helpBlock",[function(){ return{ restrict: "E", template: function(element,attrs){ var _html = ""; _html += '<span class="error text-small block" ng-if="' + attrs.target + '.$error.checkasync">命名重复</span>'; _html += '<span class="error text-small block" ng-if="' + attrs.target + '.$error.minlength">内容太短</span>'; _html += '<span class="error text-small block" ng-if="' + attrs.target + '.$dirty &&' + attrs.target + '.$error.required">不能为空</span>'; _html += '<span class="error text-small block" ng-if="' + attrs.target + '.$error.pattern">' + attrs.patternText + '</span>'; _html += '<span class="error text-small block" ng-if="' + attrs.target + '.$error.pwdmatch">两次密码不一致</span>'; _html += '<span class="error text-small block" ng-if="' + attrs.target + '.$invalid &&'+ attrs.target + '.$dirty &&'+!!attrs.text+'">' + attrs.text + '</span>'; return _html; } }; }]); //判断两次密码输入是否相同的html <div class="form-group" ng-class="{'has-error':myForm.password.$dirty && myForm.password.$invalid, 'has-success':myForm.password.$valid}"> <label> 密码 <span class="symbol required"></span> </label> <input type="hidden"> <input type="password" class="form-control" name="password" ng-model="form.password" autocomplete="off" required maxlength="20" ng-minlength="4" add-place-holder=""/> <help-block target="myForm.password"></help-block> </div> <div class="form-group" ng-class="{'has-error':myForm.cpassword.$dirty && myForm.cpassword.$invalid, 'has-success':myForm.cpassword.$valid}"> <label> 再输一次 <span class="symbol required"></span> </label> <input type="password" class="form-control" name="cpassword" ng-model="form.cpassword" autocomplete="off" required maxlength="20" ng-minlength="4" my-pwd-match="myForm.password" add-place-holder=""/> <help-block target="myForm.cpassword"></help-block> </div>
转载于:https://www.cnblogs.com/bridge7839/p/6394984.html