我注意到我无法从其方法之一访问该组件,该方法已分配给一个异步验证器(用超时进行模拟)。
因此,我需要使用bind()强制其上下文。异步验证程序是否总是这样?还是缺少其他任何东西,以便此方法将上下文保留在其中?
这是代码:
https://embed.plnkr.co/xJz7Z66SUUFaN8QCYcrJ/
注意验证器的分配:
this.asyncExampleValidator.bind(this)
在data-driven.component.ts中
您可以使用实例函数将“ this”保留在您希望的位置
这里
onAddHobby() {
(<FormArray>this.myForm.get('hobbies')).push(new FormControl('', Validators.required, this.asyncExampleValidator.bind(this)));
}
onAddHobby“ this”可能不是您所期望的,并且asyncExampleValidator遇到相同的情况
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句