2014-02-13 107 views
0

我自己做了一个自定义指令,它工作正常,但现在我得到了一个表格,其中有一些禁用的字段与ng-disabled,我相信我必须调用setTimeout函数,因为ng-disabled可能发生后,事实上,但我不知道我编码正确...是我的代码正确的方式?我不确定是否有一个特殊位置来放置setTimeout一段代码,我甚至不确定它实际上是否正确......但它确实似乎起作用......所以有人可以验证和/或更新我的代码是否需要?Angular自定义验证指令 - 如何正确跳过禁用的字段?

// Angular - custom Directive 
directive('myDirective', function($log) { 
    return { 
     require: "ngModel", 
     link: function(scope, elm, attrs, ctrl) { 
      validate = function(value) { 
       ..... 
      } 

      var validator = function(value) { 
       // invalidate field before doing validation 
       ctrl.$setValidity('validation', false); 

       elm.unbind('keyup').bind(keyup, function() { 
        // make the regular validation of the field value 
        var isValid = validate(value); // call validate method   
        scope.$apply(ctrl.$setValidity('validation', isValid));    
       }); 

       // for the case of field that might be ng-disabled, we should skip validation 
       setTimeout(function() { 
        if(elm.attr('disabled')) { 
         ctrl.$setValidity('validation', true); 
        } 
       }, 0); 

       return value;  
      }; 

      // attach the Validator object to the element 
      ctrl.$parsers.unshift(validator); 
      ctrl.$formatters.unshift(validator); 
     } 
    }; 
}); 


编辑
我必须指出,这段代码是我的代码一个非常微小的一部分,我只用了它的相关部分,并在是第一次看unbind('keyup')不作很有意义,除非你看到真正的代码更像unbind('keyup').bind(optionEvnt) ......这实际上是给出了一个额外的可选功能,选择你想要在验证器上使用的事件触发器,并且当我使用blur事件时,默认的键盘操作会产生干扰。在许多表单验证中,我倾向于使用blur事件,这就是为什么它是可选功能。
真正的代码可以在我的Github/Angular-Validation,并提供给大家使用......看看,你可能会喜欢它足以在你的代码:)

+0

Chen-Tsu Lin ...为什么当问题与AngularJS严格相关时,你会不断添加'javascript'标签?你知道还有其他的方式来获得分数,然后每天添加标签... – ghiscoding

回答

3

你似乎有很多不必要的代码在那里,除非我错过了你的实际意图。这应该工作。

// Angular - custom Directive 
directives.directive('myDirective', function($log) { 
    return { 
     require: "ngModel", 
     link: function(scope, elm, attrs, ctrl) { 

      var validate = function(value) { 
       return (value === "valid"); 
      }; 

      var validator = function(value) { 
       ctrl.$setValidity('validInput', validate(value)); 
       return value; 
      }; 

      // attach the Validator object to the element 
      ctrl.$parsers.unshift(validator); 
      ctrl.$formatters.unshift(validator); 

      // Observe the disabled attribute 
      attrs.$observe("disabled",function(disabled) { 
       if(disabled){ 
        // Turn off validation when disabled 
        ctrl.$setValidity('validation', true); 
       } else { 
        // Re-Validate the input when enabled 
        ctrl.$setValidity('validation', validate(ctrl.$viewValue)); 
       } 
      }); 

     } 
    }; 
}); 
+0

谢谢今晚我会来看看它......'keyup'的东西并不完全是我写的,我的指令有很多特性,其中一个特性是通过你想要的任何事件触发器,所以我真正的代码是'elm.unbind('keyup')。unbind(evnt).bind(evnt,function(){...}'我不得不解除绑定,因为当我使用另一个绑定时类型的事件我有一个Github提供给每个人,如果你想使用它继续... https://github.com/ghiscoding/angular-validation – ghiscoding

+0

顺便说一句,我愿意提出建议和代码更改Github以及我正处于学习Angular的初期...... thx – ghiscoding

+0

感谢您接受我的回答,现在我可以随处评论了!(我肯定他们会后悔的)。至于您的项目,它看起来很棒,您是否愿意接受要求呢? –

1

使用它,我认为,如果你使用的角度的$timeout而不是JavaScript的原生setTimeout()你会有更多的运气,因为$timeout让棱角知道发生了什么,需要更新什么和所有。不过我认为最好的解决方案你希望做的是观察指令的定时器和间隔残疾人属性,不需要什么:

attrs.$observe("disabled",function(value) { 

if(value){ 

     ctrl.$setValidity('validation', true); 

}else{ 

     ctrl.$setValidity('validation', false); 

}}); 
+0

啊是的,我总是忘记,Angular有它自己的'$超时'方法..哈哈...感谢代码我会在今晚尝试它后工作。 – ghiscoding