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
Javascript 每次发送请求时,都要取消BounceTime_Javascript_Angular_Typescript_Rxjs_Debounce - Fatal编程技术网

Javascript 每次发送请求时,都要取消BounceTime

Javascript 每次发送请求时,都要取消BounceTime,javascript,angular,typescript,rxjs,debounce,Javascript,Angular,Typescript,Rxjs,Debounce,异步验证器每次在电子邮件表单中键入时都向JSON发送请求。它检查现有的电子邮件地址,以便键入每封信。怎么能给服务器打一次电话并检查一次,而不是每封信 isEmailExist(): AsyncValidatorFn { return (control: AbstractControl): Observable<any> => { return this.usersService.getUserByEmail(control.value).pipe(

异步验证器每次在电子邮件表单中键入时都向JSON发送请求。它检查现有的电子邮件地址,以便键入每封信。怎么能给服务器打一次电话并检查一次,而不是每封信

isEmailExist(): AsyncValidatorFn  {
   return (control: AbstractControl): Observable<any> => {
     return this.usersService.getUserByEmail(control.value).pipe(
        debounceTime(2000),
        map(users => {
            if (users.some(user => user.email.toLowerCase() === control.value.toLowerCase())) {
               return { isExist: true };
            } else {
               return null;
            }
        })
     ) 
   }
  }
isEmailExist():异步验证程序fn{
返回(控件:AbstractControl):可观察=>{
返回此.usersService.getUserByEmail(control.value).pipe(
去BounceTime(2000年),
地图(用户=>{
if(users.some(user=>user.email.toLowerCase()===control.value.toLowerCase()){
返回{isExist:true};
}否则{
返回null;
}
})
) 
}
}

Merei,您可以使用
{updateOn:'blur'}
,请参阅

另一个选项是不使用验证器并检查提交中是否存在isEmailExist

另一个选择是,你在一个去公告时间内“检查”电子邮件,但其值会发生变化。 假设您有一个变量“check”和一个控件

  check=false;
  control=new FormControl('',Validators.required,this.customValidator().bind(this))
bind(this)
使您可以使用组件的变量,因此您的customValidator可以像

  customValidator(){
    return (control)=>{
    if (!this.check)  //if the variable in component is false return of(null)
      return of(null)
                      //else you call to the service
     return this.usersService.getUserByEmail(control.value).pipe(
       map(users => {
          if (users.some(user => user.email.toLowerCase() ===
                                   control.value.toLowerCase())) {
              return { isExist: true };
          } else {
             return null;
          }
     })
    }
  }
现在,在定义FormControl之后,您将订阅valueChanges

this.control.valueChanges.pipe(debounceTime(300)).subscribe(_=>{
  this.check=true;
  this.control.updateValueAndValidity({emitEvent:false});
  this.check=false;
})

在我使用傻瓜函数模拟对您的服务的调用时,
debounceTime
操作符必须在发出
keyup
事件的可观察对象之后进行管道传输。
您可以使用操作员来实现这一点。

您的问题是什么?