This answer
让我最接近正确实现这一点但是只要我的字段在表单上设置$invalid
键(至少我认为它是什么)它就会删除我的文本框中的文本
答案 0 :(得分:0)
为了使用你的指令,你需要添加priority:1000
,这样才能在任何指令执行之前执行代码。在编译DOM之前,您需要删除指令属性meta-validate
,以便在编译时不会执行。如果您不这样做,那么您将收到Maximum Call stack exceeds.
错误。
<强>指令强>
myApp.directive('metaValidate', function ($compile) {
return {
restrict: 'A',
priority: 1000, //this setting is important to make sure it executes before other directives
compile: function compile(element, attrs) {
return {
pre: function preLink(scope, iElement, iAttrs, controller) { if(!element.attr('ng-maxlength')){
element.attr('ng-maxlength', '2');
element.removeAttr("meta-validate");
} },
post: function postLink(scope, iElement, iAttrs, controller) {
$compile(element)(scope);
}
};
}
};
});
希望这可以帮到你。感谢。