[英]By clicking an item in IFrame (Inline Frame) i need to route to specific URL
Hi am using i frame in one component of my angular 2 application. 嗨,我在我的angular 2应用程序的一个组件中使用了i frame。
Component.ts 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 Component.html
<iframe src="http://sampleurl" width="100%" height="600" style="margin-top:-10px" frameborder="0"
webkitallowfullscreen mozallowfullscreen allowfullscreen>
</iframe>
While triggering a click event on i frame, i need to route to another component.(which i had already specified at my root component's routing file). 在i框架上触发click事件时,我需要路由到另一个组件。(我已经在根组件的路由文件中指定了该组件)。
Right now inside the i frame app (a mvc app) i had triggered a click function 现在在 iframe应用程序 (一个mvc应用程序)中,我已经触发了点击功能
parrent.showme()
and added index.js on root folder of angular 2 app (linked with index.html) 并在angular 2应用程序的根文件夹上添加了index.js(与index.html链接)
index.js index.js
function showme()
{
alert('function called');
FooModule.FooInstance.onSelectid();
}
now i can show alert on the angular 2 app page via clicking inside the i frame app. 现在,我可以通过在i frame应用程序内部单击来在angular 2应用程序页面上显示警报。
The target is to change the functionality of the showme() to change the route of the angular 2 app. 目标是更改showme()的功能以更改angular 2应用程序的路由。
How can i do it ? 我该怎么做 ?
i had tried several methods to solve it, unfortunately i cant get any. 我尝试了几种方法来解决它,不幸的是我什么也做不了。
Add id to your frame and then use contentWindow.document.body.onclick on iframe element 将ID添加到您的框架中,然后使用contentWindow.document.body.onclick iframe元素
<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']);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.