gpt4 book ai didi

javascript - Angular JS 自定义验证 $asyncvalidator 未定义

转载 作者:行者123 更新时间:2023-12-03 11:24:37 25 4
gpt4 key购买 nike

尝试将异步验证器添加到我的自定义用户名验证器中。但是它输出错误:

类型错误:无法设置未定义的属性“用户名”。

如何定义 asyncvalidator?我以为我会被 NgModel Controller 自动控制?

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
angular.module('myApp.commonDirectives', ['myApp.services']).directive('username', ['UsernameService',function(UsernameService){

return {

restrict: 'A',
require: "ngModel",
scope: {
hasFocus: "=username"

},
link: function(scope, element, attrs, ctrl) {

scope.$watch('hasFocus', function(hasFocus) {
if(angular.isDefined(hasFocus)) {

// on blur
if(!hasFocus) {


ctrl.$validated=false;
ctrl.$asyncValidators.username = function(value){
UsernameService.validate(value)
.then(function(resolvedData) {
if(resolvedData) {

ctrl.$validated=true;
ctrl.$setValidity('checking', true);
// ctrl.$setValidity('username', true);
}
else {

ctrl.$validated=true;
ctrl.$setValidity('checking', false);
// ctrl.$setValidity('username', false);
}
}
);
}
}

// on focus
else {
ctrl.$setValidity('username', true);
ctrl.$setValidity('checking', true);
}
}

});
}

}
}])

最佳答案

$asyncValidators 可从 angularjs 1.3 及更高版本获得:

https://code.angularjs.org/1.2.27/docs/api/ng/type/ngModel.NgModelController

关于javascript - Angular JS 自定义验证 $asyncvalidator 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26976212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com