我正在使用自定义角度指令来验证表单中的字段。
如果其中一项验证失败,并且自定义错误,则该类ng-invalid-parse
也会添加到该字段中。
这是正常行为吗?如果是这样,是什么原因造成的?
答案 0 :(得分:15)
ng-invalid-parse
,之后不再调用验证。
更多信息:AngularJS docs
答案 1 :(得分:4)
发现这是因为我的验证是正确的,但ng-invalid
类仍在应用,因为ng-invalid-parse
正在应用。
在我(简单)的情况下,我正在验证ipv4地址,并且无法返回值:
angular.module('insightApp.directives').directive('ipInput', function () {
return {
restrict: 'A',
require: '?ngModel',
link: function (scope, elem, attrs, ctrl) {
if (!(ctrl)) {
return;
}
function ipValidator(ngModelValue) {
if (
ngModelValue != '0.0.0.0' &&
ngModelValue != '255.255.255.255' &&
ngModelValue.match(/\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/)
) {
ctrl.$setValidity('ipValidator', true);
} else {
ctrl.$setValidity('ipValidator', false);
}
// Adding this return statement resolved the problem with "ng-invalid-parse" being added
return ngModelValue;
}
ctrl.$parsers.push(ipValidator);
}
}
});