Angular - ngModelController缺少$ validators?

mor*_*h84 8 validation angularjs angular-ngmodel angularjs-ng-model

我一直在看Angular文档:https: //docs.angularjs.org/guide/forms#custom-validation

我正在尝试使用自定义指令创建自己的输入字段验证器.我创建了一个与上面链接相似的指令,只使用我自己的验证功能(6位密码)进行了自定义:

app.directive('password', function() {
   return {
       require: 'ngModel',
       link: function(scope, element, attrs, ctrl) {
           ctrl.$validators.password = function (modelValue, viewValue) {
               if (/^[0-9]{6}$/.test(viewValue)) {
                   return true;
               }

               return false;
           };
       }
   };
});
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我收到此错误:

Error: ctrl.$validators is undefined
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么?

a b*_*ver 14

$validators仅从1.3版开始存在.与您的评论相反,最新的稳定版本是1.3.0.

  • [Google确实托管了它](https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0/angular.js). (2认同)