用去抖动包装Angular验证器

时间:2019-05-12 14:18:09

标签: angular typescript angular-validation debouncing

我认为在Angular中创建防抖验证器是微不足道的,但事实并非如此。是的,我知道,我可以创建自定义验证器,但我需要的是标准验证器,但已被反跳。

我有什么

'username': new FormControl('', Validators.compose([Validators.required, Validators.minLength(3)]))

我想拥有的东西

'username': new FormControl('', Validators.compose([ValidatorUtil.debounce(Validators.required), ValidatorUtil.debounce(Validators.minLength(3)])))

所以试图实现它,但我失败了:(

import {Validators} from "@angular/forms";
import {ValidatorFn} from "@angular/forms/src/directives/validators";

export class ValidatorUtil extends Validators {

static debounce(validator: (ValidatorFn | null | undefined)): ValidatorFn | null {
       return Observable.timer(1000).switchMap(()=>{
         return validator()
        .mapTo(null)
        .catch(err=>Observable.of({availability: true}));
       });
}
}

有什么提示吗?

代码改编自 How to add debounce time to an async validator in angular 2?

0 个答案:

没有答案