从 @typeform/embed createWidget onSubmit 回调中访问 angular 服务

Accessing angular service from within @typeform/embed createWidget onSubmit callback

我成功地使用 vanilla SDK 在 Angular 组件中嵌入了 Typeform。

然而,当我尝试从 onSubmit 回调函数中访问 Angular 服务时,出现以下错误。

ERROR TypeError: Cannot read properties of undefined (reading '_authService')

代码如下:

export class OnboardComponent implements AfterViewInit {

  @ViewChild('formContainer') formContainer:any;

  constructor(private _userService:UserService, private _authService: AuthService) { 
  }
  
  ngAfterViewInit(): void {
    createWidget("<<Form ID>>", { 
      enableSandbox: true,
      container: this.formContainer.nativeElement,
      onSubmit: this.formFilled,
    });
  }

  formFilled() {
    this._authService.currentUser$.subscribe(user => {
      this._userService.setUserOnboarded(user);
    });
  }
}

有什么解决办法吗?

尝试通过绑定保持上下文:

ngAfterViewInit(): void {
    createWidget("<<Form ID>>", { 
      enableSandbox: true,
      container: this.formContainer.nativeElement,
      onSubmit: this.formFilled.bind(this),
    });
  }