【发布时间】:2021-12-26 22:09:26
【问题描述】:
我正在尝试显示服务器上生成的错误消息,以便在返回到角度异步验证器后显示。
这是我的验证器:
(function () {
var asyncValidatorTest = function ($http, $q) {
return {
restrict: "A",
require: "ngModel",
link: function (scope, elem, attrs, ngModel) {
ngModel.$asyncValidators.asyncTest = function (modelValue, viewValue) {
var userInput = modelValue || viewValue;
var message = attrs[asyncValidatorTest];
return $http({
method: "POST",
url: "/AngularTest/AsyncValidatorTest",
data: { input: userInput }
})
.then(function (response) {
return true;
}, function (response) {
message = response.statusText;
return $q.reject(response.statusText);
});
}
}
}
}
angular.module("angularApp").directive("asyncValidatorTest", ["$http", "$q", asyncValidatorTest]);
}())
这里是html:
<div class="form-group" ng-class="{'has-error': (testForm.$submitted && testForm.asyncValidateTest.$invalid) || (testForm.asyncValidateTest.$invalid && testForm.asyncValidateTest.$dirty)}">
<label for="validateTest" class="control-label">Async Validate Test</label>
<input type="text" name="asyncValidateTest" class="form-control" ng-model="vt.message" ng-model-options="{updateOn: 'blur'}" async-validator-test="vt.serverError" required />
<span class="help-block" ng-show="(testForm.$submitted && testForm.asyncValidateTest.$error.required) || (testForm.asyncValidateTest.$error.required && testForm.asyncValidateTest.$dirty)">Input value is required.</span>
<div ng-show="!testForm.asyncValidateTest.$error.required">
<div ng-show="(testForm.$submitted && testForm.asyncValidateTest.$invalid) || (testForm.asyncValidateTest.$invalid && testForm.asyncValidateTest.$dirty)">
<span class="help-block">{{ vt.serverError }}</span>
</div>
</div>
</div>
我错过了什么?显然有什么。
【问题讨论】:
标签: validation angularjs-directive