形成没有ng-model的$ validators

时间:2015-05-18 12:53:10

标签: javascript angularjs validation angularjs-directive

我有一个form元素:

<form name="formDate">
    <input date-picker name="fundacao" ng-model-date="fTit.fundacao" required>
</form>

指令:

app.directive('datePicker', function(){
    return {
        restrict: 'A',
        scope: {
            ngModelDate: "=ngModelDate"
        },
        link: function (ng, el, attr) {     
            // $validators HERE
        }
    };
})

&#34; required&#34;验证不起作用,因为元素上没有ng-model指令。

有没有办法验证带有非标准指令的表单,例如上面的示例?

1 个答案:

答案 0 :(得分:0)

你可以在元素上绑定更改函数

app.directive('datePicker', function(){
return {
    restrict: 'A',
    scope: {
        ngModelDate: "=ngModelDate"
    },
    link: function (ng, el, attr,ctrl) {     
        el.bind('change',function(){
          //check the $(el).val();
         var isValid= the date is valid;
         ctrl.$setValidity('date',isValid);
         // the first param is the validation key, the second param is true if valid false if invalid
    })
   }
 };
})