【发布时间】:2015-07-28 17:39:37
【问题描述】:
我正在尝试将 ng-minlength 和 ng-maxlength 添加到表单输入字段。发生了一些奇怪的事情。当输入的数字小于最小长度时,它会消失并且永远不会添加到模型中。
但是,如果您输入超过 13 位的数字,它会呈现但不会添加到模型中。
app.directive('cmNumber', ['$compile', function ($compile) {
return {
restrict: 'A',
compile: function (element) {
var onlyNumbers = /^\d+$/;
element.attr('ng-minlength', '13');
element.attr('ng-maxlength', '19');
element.attr('ng-pattern', onlyNumbers);
element.attr('placeholder', '123456789112345');
element.removeAttr('cm-Number'); //remove the attribute to avoid infinite loop
return function (scope, element) {
$compile(element[0].form)(scope);
};
}
};
}]);
【问题讨论】:
-
你为什么使用
compile而不是将这些值添加到模板中?
标签: angularjs