gpt4 book ai didi

Angular 5 Debounce 自定义异步验证器

转载 作者:行者123 更新时间:2023-12-02 00:28:44 106 4
gpt4 key购买 nike

我已经创建了一个自定义异步验证器,它使用一项服务来根据服务器验证电子邮件。然而,这意味着每次输入一个字符时服务器都会被命中,这是不好的。我在这里遵循了几个我无法开始工作的答案。

我的验证器:

import {FormControl, NG_ASYNC_VALIDATORS, Validator} from 
'@angular/forms';
import { Http } from '@angular/http';
import {Directive, forwardRef} from "@angular/core";
import {ValidateEmailService} from "../services/validate-email.service";
import {UserService} from "../services/user.service";

@Directive({
selector: '[appEmailValidator]',
providers: [
{ provide: NG_ASYNC_VALIDATORS, useExisting: forwardRef(() => EmailValidator), multi: true }
]
})
export class EmailValidator implements Validator {
public validateEmailService: ValidateEmailService;

constructor(
private _http: Http,
private _userService: UserService
) {
this.validateEmailService = new ValidateEmailService(this._http, this._userService);
}

validate(c: FormControl) {
return new Promise(resolve => {
this.validateEmailService.validateEmail(c.value)
.subscribe((res) => {
console.log(res);
if (res.valid) {
resolve(null);
} else {
resolve({
valid: {
valid: false
}
});
}
});
})
}
}

它本身运行良好,但一旦我尝试向它添加某种形式的去抖动,我最终会破坏它。

我试过来自 this question 的答案我得到了类似 Type X is not assignable to type 'Observable<any>' 的错误等等

我通过使用 setTimeout 接近了但最终所做的只是停止该功能。

我的最终目标是仅在大约 600 毫秒内未更改输入时运行验证器,但满足于每 600-2000 毫秒仅验证一次。

为了更加清楚,validateEmail来自 ValidateEmailService 的方法:

public validateEmail(email: string) {

let validateEmail = new ValidateEmail(email);

return this._http.get(
this.getUrl(validateEmail),
this.getOptionArgs())
.map((response: Response) => Object.assign(new UserEmailVerification(), response.json().UserEmailVerification));

}

最佳答案

我还没有看到异步验证器作为指令实现,而是作为分配给表单控件的验证器函数实现的。

这是我在类似情况下使用的示例验证器:

import { Injectable } from '@angular/core';
import { AbstractControl, AsyncValidatorFn } from '@angular/forms';
import { Observable, timer, of } from 'rxjs';
import { switchMap, map } from 'rxjs/operators';

import { MembershipsService } from '@app/memberships/memberships.service';

@Injectable()
export class MembershipsValidators {

constructor (
private membershipsService: MembershipsService,
) {}

checkMembershipExists(email?: string): AsyncValidatorFn {
return (control: AbstractControl): Observable<{ [key: string]: any } | null> => {
if (control.value === null || control.value.length === 0) {
return of(null);
}
else if (email && email === control.value) {
return of(null);
}
else {
return timer(500).pipe(
switchMap(() => {
return this.membershipsService.lookupMember(control.value).pipe(
map(member => {
if (!member) {
return { noMembership: { value: control.value } };
}

return null;
})
);
})
);
}
};
}

}

它被导入并应用到表单控件中:

this.form = this.formBuilder.group({
memberEmail: new FormControl('', {
validators: [ Validators.required, Validators.pattern(regexPatterns.email) ],
asyncValidators: [ this.membershipsValidators.checkMembershipExists() ],
}),
});

这样,在同步验证器得到满足之前,异步验证器不会触发。

关于Angular 5 Debounce 自定义异步验证器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52782674/

106 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com