【发布时间】:2015-11-09 05:35:14
【问题描述】:
我编写了自定义 AngularJS 指令,动态添加 ng-pattern 并做一些其他事情。
它可以工作,但在 Chrome 和 Internet Explorer 中,如果用户尝试在现有字符串的中间输入字符,光标会跳转到字符串的末尾。在 Firefox 中它工作正常。
(使用 Chrome 44、Firefox 40、IE 11 测试)
HTML:
<input type="text" name="input1" ng-model="value1" validation-directive>
JS:
myApp.directive("validationDirective", function ($compile) {
return {
restrict: 'A',
link: function (scope, element) {
element.removeAttr('validation-directive'); // necessary to avoid infinite compile loop
element.attr("ng-pattern", new RegExp("^[a-z]{0,10}$"));
//Do more stuff...
$compile(element)(scope);
}
};
});
为什么会这样?谁能解决这个问题?
谢谢!
【问题讨论】:
-
所以你的意思是如果你没有验证指令它工作正常?
-
如果您真的想编写自定义验证,您应该在指令中要求 ngModel 并将其添加到
$validators对象,但如果这对您来说足够好,也许您可以提供一个小提琴? -
@Mikey 我们可以使这个指令起作用..只需按照我在回答中所做的操作
标签: angularjs google-chrome internet-explorer angularjs-directive