javascript - 通过单击 IFrame(内联框架)中的项目,我需要路由到特定的 URL

标签 javascript angular iframe angular2-router3

您好,我正在我的 Angular 2 应用程序的一个组件中使用 i 框架。

Component.ts

import { Component, OnInit } from '@angular/core';
import { SafeResourceUrl,DomSanitizer,} from '@angular/platform-browser';

@Component({
  selector: 'sample comp',
  templateUrl: './sample-comp.component.html',
  styleUrls: ['./sample-comp.component.css']
})
export class sampleComp implements OnInit {
  //DECLARATION
  webURL:SafeResourceUrl;
  constructor() { 

  }

  ngOnInit() {
  }
public onSelectid(){
  console.log('Router selected');

}
}
let greeter = new CampaignOrderComponent();
module FooModule {  
    export var FooInstance = new CampaignOrderComponent();
}

Component.html

<iframe src="http://sampleurl" width="100%" height="600" style="margin-top:-10px" frameborder="0"
    webkitallowfullscreen mozallowfullscreen allowfullscreen>
</iframe>

在 i 框架上触发点击事件时,我需要路由到另一个组件。(我已经在根组件的路由文件中指定了该组件)。

现在在i框架应用程序内(一个mvc应用程序)我触发了一个点击函数

parrent.showme()

并在 Angular 2 应用程序的根文件夹中添加了index.js(与index.html链接)

index.js

  function showme()
  {
       alert('function called');
       FooModule.FooInstance.onSelectid();

  }

现在我可以通过单击 i 框架应用程序内部来在 Angular 2 应用程序页面上显示警报。

目标是更改 showme() 的功能以更改 Angular 2 应用程序的路线。

我该怎么做?

我尝试了多种方法来解决这个问题,不幸的是我无法得到任何结果。

最佳答案

将 id 添加到框架中,然后在 iframe 元素上使用 contentWindow.document.body.onclick

   <iframe id="iframe_id" src="https://sampleurl" style="margin-top:-10px" frameborder="1"
        webkitallowfullscreen mozallowfullscreen allowfullscreen>
    </iframe>

 constructor(private _router: Router){
  } 
  ngAfterViewInit(){
    document.getElementById("iframe_id").contentWindow.document.body.onclick = 
      () => {
      this._router.navigate(['/yourChildRoute']);
      }


   }

关于javascript - 通过单击 IFrame(内联框架)中的项目,我需要路由到特定的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39889580/

相关文章:

php - 通过 PHP 集成 Yodlee FaSTLink

html - 页面更改时是否可以将客户端状态保存在 iFrame 中?

javascript - 为我的 slider 实现导航点?

css - Angular NgStyle : list image style

angular - 在运行时加载 Angular 模板

Angular 5.x 页面加载或模块本身的初始化需要更长的时间

javascript - 如何保存 svg 文件捕获的签名(以 HTML 形式呈现)

javascript - RxJS 与最后一个比较并发出

javascript - 当用户输入时,在同一页面上以 HTML 格式显示 FCKeditor 输入数据?

javascript - 如何在 Node 中调试基于 promise 的代码?