无法获得多个Angular Directives以进行表单验证

时间:2014-09-30 01:36:34

标签: angularjs angularjs-directive

我有一个带有两个输入控件的表单,每个控件都有一个不同的验证指令:

<input name='foo' id='foo' ng-model='foobar' validate-foo>
<input name='bar' id='bar' ng-model='foobar' validate-bar>

foo指令:

app.directive('validateFoo', function(){
return{
    restrict: 'A',
    require: 'ngModel',
    link: function(scope, elem, attr, ctrl) {

        ctrl.$parsers.unshift(function (viewValue) {
            var id= attr.id;
            if(viewValue.length > 0) {
                console.log("*** foo is not empty");
            }
            else{
                console.log("*** foo is empty");
            }
        });
    }
}
});

指示栏:

app.directive('validateBar', function(){
return{
    restrict: 'A',
    require: 'ngModel',
    link: function(scope, elem, attr, ctrl) {

        ctrl.$parsers.unshift(function (viewValue) {
            var id= attr.id;
            if(viewValue.length > 0) {
                console.log("*** bar is not empty");
            }
            else{
                console.log("*** bar is empty");
            }
        });
    }
}
});

呈现页面时,我得到的错误如下:

Error: [$compile:ctreq] http://errors.angularjs.org/1.3.0-beta.5/$compile/ctreq?p0=ngModel&p1=valida

我可以在一个表单中没有多个验证指令吗?

这是我的指令,它执行动态html,编译部分......

html= <input name=......, etc. etc.';
var dom= angular.element(html);
$element.append(dom);
$compile(dom)($scope);

1 个答案:

答案 0 :(得分:0)

我失踪了&#39; ngModel&#39;在动态生成的html中