如何使用Reactive Forms(Angular2 RC3)进行异步验证?

时间:2016-09-09 12:12:47

标签: javascript validation asynchronous angular

我发现这个tutorial可以进行异步验证。这不再适用于RC3(Reactive Froms)。

如何在此进行异步验证?

this.username = new FormControl( '', [ <any>Validators.required, <any>ProfileValidator.usernameTaken ]);

...

class ProfileValidator {

 static usernameTaken(control: FormControl): Promise<{[key:string]:boolean}> {

   let q = new Promise((resolve, reject) => {
     setTimeout(() => {
       resolve(null);
     }, 1000)
   });

   return q;
 }

}

FormControl始终无效......

1 个答案:

答案 0 :(得分:0)

表单控件的签名应该像第三个arg那样具有异步验证器:

new FormControl( '', 
    [ <any>Validators.required], //sync
    [<any>ProfileValidator.usernameTaken ] //async
    );