iframe触发功能问题

时间:2018-10-03 11:04:06

标签: javascript angular typescript iframe

在Angular组件中使用iframe,在iframe中有一个用户列表,当我单击用户时,有什么方法可以触发一个函数,该函数是否位于组件.ts文件中。

1 个答案:

答案 0 :(得分:0)

iframe和父文档之间无法进行跨源通信 如果源相同,则可以在同一窗口对象中发送/广播消息并接收消息。从接收方消息中,您可以调用自定义消息。

资源。

Template :
    <iframe src="http://javascript.info" name="example">

Component : 
    let win = window.frames.example;

    win.postMessage("message", "http://example.com");

接收广播消息:

window.addEventListener("message", function(event) {
  if (event.origin != 'http://javascript.info') {
    // something from an unknown domain, let's ignore it
    return;
  }

  alert( "received: " + event.data );
});