所以我有一个像这样的自定义异步验证器:
import { Injectable } from '@angular/core';
import { FormControl } from "@angular/forms";
import { DefaultApi } from "../data/api/DefaultApi";
import { Observable } from 'rxjs/Rx';
import 'rxjs/Rx';
@Injectable()
export class ValidationService {
tempURL = "http://localhost/api/v1/validate";
oldVal: any;
constructor(private defaultApi: DefaultApi) {
}
generateValidator(validationType: string) {
return (control: FormControl) => {
return new Observable((obs: any) => {
control
.valueChanges
.debounceTime(400)
.mergeMap(value => {
return this.defaultApi.validationGet(validationType, value)
})
.subscribe(
data => {
obs.next(null);
obs.complete();
},
error => {
obs.next({'validationFailed': true});
obs.complete();
}
);
}).distinctUntilChanged();
}
}
}
问题是虽然它正在分组'击键正确(即有人输入913 913而不是9 1 3)它会多次发射它们(913,913,913)。我试过移动distinctUntilChanged但无济于事。我错过了一些明显的东西吗?
答案 0 :(得分:0)
尝试使用.throttleTime(400)
代替.debounceTime(400)
。