【发布时间】:2020-04-10 13:03:48
【问题描述】:
当我出于某种原因在表单输入上按下一个键时,异步验证器不会检测到 distinctUntilChanged,它仍然会发送 API 请求。 例如,如果我按 35,删除 5,然后再次添加 5,它仍然会发送请求。 这是代码: (我已经尝试了几乎所有方法,但仍然无法正常工作)
validateSomeNumber(control: FormControl): Observable<any> | Promise <any> {
this.isSubmitBtnDisabled = true;
return control.valueChanges.pipe(
debounceTime(1000),
distinctUntilChanged(),
switchMap((value) => {
return this.apiService.someApiRequest({ 'to_number': control.value }).pipe(
map(res => {
console.log(res);
if (res.success) {
// console.log(res);
this.isSubmitBtnDisabled = false;
return null;
} else {
// console.log(res);
this.isSubmitBtnDisabled = true;
return{ 'invalidCharacters': true };
}
}),
);
}),
first()
);
}
【问题讨论】:
-
您确定不是在每次按键更改时都调用此方法吗?因为这样每次都会创建一个新链。
-
你每次都返回新的 Observable,
distinctUntilChanged()不起作用。
标签: validation asynchronous rxjs operators angular-reactive-forms