How to debounce async validator in Angular 4 with RxJS observable?

后端 未结 5 1007
孤城傲影
孤城傲影 2020-12-17 10:23

I\'m using custom async validator with Angular 4 reactive forms to check if E-Mail address is already taken by calling a backend.

However, Angular calls the validato

5条回答
  •  执笔经年
    2020-12-17 10:33

    I think your method only delay, not debounce, then find the sample way to archive this result.

    import { debounce } from 'lodash';
    
    ...
    
    constructor() {
       this.debounceValidate = debounce(this.debounceValidate.bind(this), 1000);
    }
    
    debounceValidate(control, resolve) {
       ...//your validator
    }
    
    validate (control: AbstractControl): Promise {
      return new Promise(resolve => {
        this.debounceValidate(control, resolve);
      })
    }
    

提交回复
热议问题