angular 指令梳理 —— 前端校验

angular js内置校验的扩展

校验成功则 scope.formName.$valid=true

校验失败  元素的class: ng-invalid

成功:.ng-valid

/**
 * 校验指令. <input ng-model="data.user" ng-validate="{require:true,number:true}" > 
 */
.directive('htValidate', [function () {
      return {
          require: "ngModel",
          link: function (scope, element, attr, ctrl) {
              var validate = attr.htValidate;
              var permission=getPermission(attr.permission,scope);//权限。
              //如果没有必填权限且没有其他校验返回
              if(permission!=="b" && validate=="{}")return;
             var validateJson = eval('(' + validate + ')');if(permission=="b")validateJson.required = true;//如果必填权限。默认必填
              
              var customValidator = function (value) {
                  if(!validate) return true;
          handlTargetValue(validateJson);//特殊处理可忽略
var validity = $.fn.validRules(value,validateJson,element);//调用系统校验,做出错提示返回true/false ctrl.$setValidity("customValidate", validity); return validity ? value : undefined; }; //modelValue To ViewValue 的时候不通过校验 则不展示 ctrl.$formatters.push(customValidator); ctrl.$parsers.push(customValidator); //特殊的一些处理 可忽略 //获取比较目标字段的值。 所有比较的都包含target对象eg:{eq:{target:data.mian.name}} var handlTargetValue = function(validateJson){ for(key in validateJson){ if(validateJson[key].target){ validateJson[key].targetVal =eval("scope."+dateRange.target); } } } } }; }])

 精简版:

 1 .directive('htValidate',function () {
 2       return {
 3           require: "ngModel",
 4           link: function (scope, element, attr, ctrl) {
 5               var validate = attr.htValidate;
          if(!validate)return;
6 var validateJson = eval('(' + validate + ')'); 11 18 var customValidator = function (value) {
22           var validity = $.fn.validRules(value,validateJson,element); //return true/false 21 ctrl.$setValidity("customValidate", validity); 23 return validity ? value : undefined; 24 }; 25 26 ctrl.$formatters.push(customValidator); 27 ctrl.$parsers.push(customValidator); 39 } 40 }; 41 })

 

出错可以通过qtip等进行提示。边框也可以根据错误样式做出一些改变eg:

 1  input.ng-valid,textarea.ng-valid ,select.ng-valid ,.edui-editor.ng-valid,.ng-invalid .ht-input {
 2     border-color: #5eb95e !important;
 3     -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
 4     box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
 5 } 
 6 .ng-dirty input{ 
 7     background-color: white; 
 8 } 
 9 
10 input.ng-invalid,textarea.ng-invalid, select.ng-invalid, .edui-editor.ng-invalid ,span.ng-invalid input,.ng-invalid .ht-input
11 {
12     border-color: #dd514c !important;;
13     -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
14     box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
15 }
16 div.ng-invalid label, span.ng-invalid a{
17     color:red;
18 }
19 
20 table.ng-invalid {
21     border-color: #dd514c !important;
22     -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
23     box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
24 }
 ng-valid ng-invalid CSS

$.fn.validRules(value,validateJson,element); 这部分代码。只有一半是本人写的。就不拿出来了。就是校验+qtip 提示。

 

posted @ 2016-04-30 23:17  沉毅寡言  阅读(956)  评论(1编辑  收藏  举报