Angular2 Custom Form Validators losing access to `this` in class

后端 未结 1 387
礼貌的吻别
礼貌的吻别 2021-01-04 02:22

I\'m building an Angular 2 application (2.0) and I\'ve got a form that makes use of the ReactiveForms module. As such, I\'m using validator functions on the in

1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-04 03:22

    Your validator is not being called with your component as this. Notice that you are passing a loose reference to this.userNameAsyncValidator You can bind your validator without worrying about this conflicts because angular validation doesn't rely on this

    userName = new FormControl("", [
        Validators.required
    ], [
        this.userNameAsyncValidator.bind(this)
    ]);
    

    0 讨论(0)
提交回复
热议问题