AngularJS 针对资源的自定义验证
AngularJS custom validation against a resource
我有一个 angular 表单,可以在任何表单字段更改时对所有字段执行自定义验证。每个值的有效性都可以根据表单中其他值的值而改变,因此每次编辑都会验证整个表单。
验证工作正常,但正在编辑的表单域在每次执行验证(每次按键)时都会失去焦点。我很确定这是我的验证器实现中的一个缺陷(几个月前我在互联网上的某个地方复制了它,现在找不到了)。
问题出现在 Chrome,但在 IE9 中没有。
下面是实现。有什么明显的我在这里做错了,这会导致我遇到焦点问题吗?
angular.module('myComponent').directive('myValidator', function (MyApiResource) {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, ele, attrs, ctrl) {
function myValidator(value) {
if (value) {
var myObject = JSON.parse(JSON.stringify(scope.myObject));
myObject[attrs.name] = value;
MyApiResource.validate(myObject, function (uiValidationFailures) {
for (var attributeName in myObject) {
if (scope.correctionForm[attributeName]) {
var valid = !uiValidationFailures.filter(function (el) { return el.AttributeName === attributeName; }).length;
scope.correctionForm[attributeName].$setValidity('myValidator', valid);
if (valid && attributeName !== attrs.name && scope.correctionForm[attributeName].$pristine) {
scope.correctionForm[attributeName].$setViewValue(myObject[attributeName]);
}
scope.uiValidationFailures[attributeName] = valid
? undefined
: uiValidationFailures.filter(function (el) { return el.AttributeName === attributeName; });
}
}
return value;
});
return value;
}
scope.uiValidationFailures[attrs.name] = undefined;
return undefined;
};
ctrl.$parsers.unshift(myValidator);
ctrl.$formatters.unshift(myValidator);
}
}
});
虽然我在那里找不到您的代码有任何问题(没有进一步的上下文),但当您说
时会响起警钟
The problem occurs in Chrome, but not in IE9.
另外,您说的是表单域失去焦点。我的猜测是可能会有一些不一致——也许是在焦点行为上? - 在两个浏览器之间。这不太可能是 JavaScript 问题。
我有一个 angular 表单,可以在任何表单字段更改时对所有字段执行自定义验证。每个值的有效性都可以根据表单中其他值的值而改变,因此每次编辑都会验证整个表单。
验证工作正常,但正在编辑的表单域在每次执行验证(每次按键)时都会失去焦点。我很确定这是我的验证器实现中的一个缺陷(几个月前我在互联网上的某个地方复制了它,现在找不到了)。
问题出现在 Chrome,但在 IE9 中没有。
下面是实现。有什么明显的我在这里做错了,这会导致我遇到焦点问题吗?
angular.module('myComponent').directive('myValidator', function (MyApiResource) {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, ele, attrs, ctrl) {
function myValidator(value) {
if (value) {
var myObject = JSON.parse(JSON.stringify(scope.myObject));
myObject[attrs.name] = value;
MyApiResource.validate(myObject, function (uiValidationFailures) {
for (var attributeName in myObject) {
if (scope.correctionForm[attributeName]) {
var valid = !uiValidationFailures.filter(function (el) { return el.AttributeName === attributeName; }).length;
scope.correctionForm[attributeName].$setValidity('myValidator', valid);
if (valid && attributeName !== attrs.name && scope.correctionForm[attributeName].$pristine) {
scope.correctionForm[attributeName].$setViewValue(myObject[attributeName]);
}
scope.uiValidationFailures[attributeName] = valid
? undefined
: uiValidationFailures.filter(function (el) { return el.AttributeName === attributeName; });
}
}
return value;
});
return value;
}
scope.uiValidationFailures[attrs.name] = undefined;
return undefined;
};
ctrl.$parsers.unshift(myValidator);
ctrl.$formatters.unshift(myValidator);
}
}
});
虽然我在那里找不到您的代码有任何问题(没有进一步的上下文),但当您说
时会响起警钟The problem occurs in Chrome, but not in IE9.
另外,您说的是表单域失去焦点。我的猜测是可能会有一些不一致——也许是在焦点行为上? - 在两个浏览器之间。这不太可能是 JavaScript 问题。