简体   繁体   English

在 iframe 中运行的 Angular 应用程序之外检测点击

[英]Detect click outside of Angular application which runs in an iframe

I'm developing an Angular App which is embedded in a Wordpress page.我正在开发一个嵌入在 Wordpress 页面中的 Angular 应用程序。 I'm using an iframe to embedd the Angular App into the Worpress page.我正在使用 iframe 将 Angular 应用程序嵌入 Worpress 页面。 Now I have modal dialogs in my Angular App which should be closed when clicking outside the dialogs.现在我的 Angular 应用程序中有模态对话框,在对话框外单击时应将其关闭。 That works fine when the click happens outside the dialog and inside the Angular App.当单击发生在对话框外部和 Angular 应用程序内部时,效果很好。 But when the click goes even outside the iframe, somewhere else on the wordpress page, nothing happens.但是,当点击甚至超出 iframe,即 wordpress 页面上的其他地方时,什么也没有发生。 It seems like all clicks outisde the iframe are not recognized by the Angular event handlers.似乎 iframe 事件处理程序无法识别 iframe 之外的所有点击。
I've found some solutions for Javascript where the page outside of the iframe was referenced through document.parentElement .我找到了 Javascript 的一些解决方案,其中 iframe 之外的页面是通过document.parentElement引用的。 In my case that doesn't seem to work, the parentElement of my document is undefined .就我而言,这似乎不起作用,我文档的 parentElement 是undefined
I hope you understand what my problem is, it would be quite some effort to put up a running example.我希望你明白我的问题是什么,提出一个正在运行的例子会很费力。

Regards and thanks in advance!提前致以问候和感谢!

Thanks @Msk Satheesh and @MikeOne, with the hints of both of you guys combined I got it running!感谢@Msk Satheesh 和@MikeOne,结合你们两个的提示,我让它运行起来了!
The thing is that I can really address the (outer) wordpress page by window.parent .问题是我真的可以通过window.parent寻址(外部)wordpress 页面。 But this only works because both run in the same domain.但这只有效,因为两者都在同一个域中运行。 Combined with the code Msk Satheesh posted in his first comment it works.结合 Msk Satheesh 在他的第一条评论中发布的代码,它起作用了。 And I don't even need to run anything outside of Angular. In my modal dialog component in Angular I put the following code:而且我什至不需要在 Angular 之外运行任何东西。在 Angular 的模态对话框组件中,我输入了以下代码:

  constructor(public dialogRef: MatDialogRef<ResultsDialogComponent>, private eRef: ElementRef) {}
  
  ngOnInit(): void {
    if(!this.onDocumentClickFunction){
      this.onDocumentClickFunction = this.onDocumentClick.bind(this);
    }
    window.parent.addEventListener( 'click', this.onDocumentClickFunction ); 
  }

  onDocumentClick() {    
    this.dialogRef.close();
    this.eRef.nativeElement.click();
  }

  ngOnDestroy() {
    window.parent.removeEventListener( 'click', this.onDocumentClickFunction ); 
  }

And that's it.就是这样。 The elegant thing is that the onDocumentClick() only fires if the click is outside the iFrame. That means I don't need additional logic to find out if the click is inside or outside the iFrame. The line this.eRef.nativeElement.click();优雅的是onDocumentClick()仅在点击在 iFrame 之外时触发。这意味着我不需要额外的逻辑来确定点击是在 iFrame 的内部还是外部this.eRef.nativeElement.click();this.eRef.nativeElement.click(); is necessary to bring back the focus to the iFrame. Without that line the modal only disappears if the user clicks into the iFrame himself.有必要将焦点带回 iFrame。如果没有该行,只有当用户自己点击 iFrame 时,模式才会消失。

Try Using:尝试使用:

Angular Component Angular 组件

   constructor(private zone:NgZone) {
       window['clickEvent'] = (event) => {
           zone.run(() => {
               console.log(event);
           });
       };
   }

Outer Js:外部 Js:

onClickEvent(event){
     window.parent.frames[#frameId].clickEvent(event);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM