如何在角度2中为异步验证器增加去抖动时间? [英] How to add debounce time to an async validator in angular 2?
本文介绍了如何在角度2中为异步验证器增加去抖动时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的异步验证器,没有去抖时间,如何添加呢?
This is my Async Validator it doesn't have a debounce time, how can I add it?
static emailExist(_signupService:SignupService) {
return (control:Control) => {
return new Promise((resolve, reject) => {
_signupService.checkEmail(control.value)
.subscribe(
data => {
if (data.response.available == true) {
resolve(null);
} else {
resolve({emailExist: true});
}
},
err => {
resolve({emailExist: true});
})
})
}
}
推荐答案
实现这一点实际上很简单(这不适用于您的情况)但这是一般示例)
It is actually pretty simple to achieve this (it is not for your case but it is general example)
private emailTimeout;
emailAvailability(control: Control) {
clearTimeout(this.emailTimeout);
return new Promise((resolve, reject) => {
this.emailTimeout = setTimeout(() => {
this._service.checkEmail({email: control.value})
.subscribe(
response => resolve(null),
error => resolve({availability: true}));
}, 600);
});
}
这篇关于如何在角度2中为异步验证器增加去抖动时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文