【问题标题】:angular 2 handle click on link inside iframeangular 2手柄点击iframe内的链接
【发布时间】:2017-10-29 19:58:35
【问题描述】:

有没有办法捕获对iframe 内链接的点击?

<iframe *ngIf="currentFrameUrl" id="contentFrame" [src]="currentFrameUrl | safe"></iframe>

我的 iframe 组件只包含一个订阅可观察变量的简单代码。

export class ContentFrameComponent implements OnInit {
    currentFrameUrl: string;
    currentMenuState: boolean;
    @ViewChild('contentFrame') iframe: ElementRef;

    constructor(private frameService: ContentFrameService) {        
      this.currentMenuState = true;      
    }

    ngOnInit(): void {
      this.frameService.providerCurrentUrl$.subscribe(data => this.currentFrameUrl = data);
      this.frameService.providerMenuState$.subscribe(data => this.currentMenuState = data);
    }

    ngAfterViewInit() {
      let doc = this.iframe.nativeElement.contentDocument ||this.iframe.nativeElement.contentWindow;
      if (typeof doc.addEventListener !== undefined) {
        console.log("inside if - addEventListener") // Is shown
        doc.addEventListener("click", this.iframeClickHandler, false)
      } else if (typeof doc.attachEvent !== undefined) {
        console.log("inside if - attachEvent ") // Does not show
        doc.attachEvent("onclick", this.iframeClickHandler)
      }
    }

    iframeClickHandler(): void {
      console.log("Click test") // Does not show
    }
}

我的目标是捕捉iframe 链接上的点击事件,停止其传播并使用router.navigate 或location.go/location.replaceState 设置iframe 链接的url。还不确定哪个选项更好。

我无法控制iframe 中加载的页面。我只知道,链接应该包含可以与我的动态路由一起使用的url。

【问题讨论】:

  • iframe 是否与主机页面来自同一个域?
  • 是的,我正在处理 Intranet 应用程序的覆盖。

标签: html angular typescript iframe


【解决方案1】:

使用 @HostListener('window:blur', ['$event']) 装饰器,在 IFrame 点击时附加此装饰器触发器的方法。

  @HostListener('window:blur', ['$event'])
  onWindowBlur(event: any): void {
    console.log('iframe clicked');
  }

注意:这也适用于跨域操作。

【讨论】:

    【解决方案2】:

    为什么不能使用简单的onclick?

    试试这个代码:

    <iframe *ngIf="currentFrameUrl" id="contentFrame" [src]="currentFrameUrl | safe" (onclick)="clicked()"></iframe>
    

    点击

    【讨论】:

    • 我试过了,但看起来什么都没有发生。我在单击的函数中有console.log 调用,但它没有显示。
    • 尝试将指令与@Hostlistener 一起使用。
    【解决方案3】:
    <iframe *ngIf="currentFrameUrl" id="contentFrame" [src]="currentFrameUrl | safe" #iframe></iframe>
    

    和

    export class ContentFrameComponent implements OnInit {
          currentFrameUrl: string;
          currentMenuState: boolean;
          @ViewChild('iframe') iframe: ElementRef;
    
          constructor(private frameService: ContentFrameService) {
            this.currentMenuState = true;
          }
    
          ngOnInit(): void {
            this.frameService.providerCurrentUrl$.subscribe(data => this.currentFrameUrl = data);
            this.frameService.providerMenuState$.subscribe(data => this.currentMenuState = data);
          }
    
          ngAfterViewInit() {
            let doc = this.iframe.nativeElement.contentDocument || this.iframe.nativeElement.contentWindow;
            if (typeof doc.addEventListener !== undefined) {
              doc.addEventListener("click", this.iframeClickHandler, false)
            } else if (typeof doc.attachEvent !== undefined) {
              doc.attachEvent("onclick", this.iframeClickHandler);
            }
          }
    
          iframeClickHandler() {
            alert("Iframe clicked");
          }
        }
    

    注意:这可能不适用于跨域操作。

    【讨论】:

    • 它编译,没有显示错误,但是当我在addEventListener 中添加console.log() 时,它显示但iframeClickHandler 没有显示任何内容。
    • 经过测试,代码按我的预期工作。我们错过了什么吗?
    • 注意#contentFrame。iframe 标记应为&lt;iframe *ngIf="currentFrameUrl" #contentFrame [src]="currentFrameUrl | safe"&gt;&lt;/iframe&gt;
    • 所以我能够看到“点击测试”日志,同时在加载时疯狂地点击iframe。完全加载后,我再也看不到日志了。
    • 你能试试这个吗? &lt;iframe *ngIf="currentFrameUrl" #contentFrame (load)="onIframeLoad()" [src]="currentFrameUrl | safe"&gt;&lt;/iframe&gt; 并将ngAfterViewInit的内容放到onIframeLoad中
    猜你喜欢
    • 1970-01-01
    • 2011-05-23
    • 2011-02-13
    • 2018-12-12
    • 1970-01-01
    • 2016-10-20
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多