Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度异步验证器去盎司输入_Angular_Forms_Validation_Rxjs_Observable - Fatal编程技术网

Angular 角度异步验证器去盎司输入

Angular 角度异步验证器去盎司输入,angular,forms,validation,rxjs,observable,Angular,Forms,Validation,Rxjs,Observable,我想为我的angular form组编写一个自定义异步验证器,它的任务是检查url是否可访问。但是,如果我对AbstractControl中更改的值进行去抖动,该控件总是以某种方式无效。 这是到目前为止我的代码 export class UrlValidator { static createValidator(http: HttpClient) { return (control: AbstractControl) => { const url = control

我想为我的angular form组编写一个自定义异步验证器,它的任务是检查url是否可访问。但是,如果我对AbstractControl中更改的值进行去抖动,该控件总是以某种方式无效。 这是到目前为止我的代码

export class UrlValidator {
static createValidator(http: HttpClient) {
    return (control: AbstractControl) => {
        const url = control.value;

        return http.get(url).pipe(
            throttleTime(1500),
            catchError(err => {
                console.log('err', err);
                if (err.status && err.status === 200) return of(null);
                return of({ input: 'urlError' });
            })
        );
    };
}
}
debouncetime呼叫目前没有任何作用


提前感谢

您可能需要在验证器之外进行去抖动。如果输入源持续发出,验证器将被连续调用,在http调用之后放置去Bounce不会做任何事情

export class UrlValidator {
static controlValue=new Subject()
static createValidator(http: HttpClient) {
    UrlValiator.controlValue.next(control.value)
    return (control: AbstractControl) => {
        return controlValue.pipe(
            debounceTime(1500),
            switchMap(()=>http(url))
            catchError(err => {
                console.log('err', err);
                if (err.status && err.status === 200) return of(null);
                return of({ input: 'urlError' });
            })
        );
    };
}
}

好吧,问题是,可观察对象被困在挂起状态。
.first()
添加到http observable中,成功了

您可以提供更多的代码(尤其是您的表单?)吗?首先,我建议将http请求和hist逻辑移动到单独的服务中,处理此内部组件/指令是一种糟糕的做法。