Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/427.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从iframe调用函数_Javascript_Angular_Iframe - Fatal编程技术网

Javascript 如何从iframe调用函数

Javascript 如何从iframe调用函数,javascript,angular,iframe,Javascript,Angular,Iframe,我在iframe中放了一个angular应用程序,我想调用他的函数 <iframe #iframe frameborder="0" src="http://localhost:4200/edit/username"></iframe> 我的目标是从iframe调用PageEditorComponent的任何函数,另一种方法是使用Window.postMessage API从iframe内部到外部进行通信。您的角度代码必须查找消息/事件。它更像是用于双方通信的消息总线。您

我在iframe中放了一个angular应用程序,我想调用他的函数

<iframe #iframe frameborder="0" src="http://localhost:4200/edit/username"></iframe>

我的目标是从iframe调用PageEditorComponent的任何函数,另一种方法是使用Window.postMessage API从iframe内部到外部进行通信。您的角度代码必须查找消息/事件。它更像是用于双方通信的消息总线。

您的组件是否在iframe中加载?是的,一切正常。在这种情况下,ngOnInit方法应该在组件加载时启动。这不是被解雇了吗?是的,但我在iframe下有一个保存按钮,我的目标是当我点击这个按钮时,我想保存数据,而函数save在iframe中是的。因此,您试图从iframe内部到外部进行交互。在这种情况下,窗口对象具有可用于实现此事件的方法。点击save可使用window.postMessage发布事件。而你的代码的另一面必须对此保持警惕。然后调用实际的save方法。希望没有混乱?
export class PageEditorComponent implements OnInit...{
 @Input() eventMessage = { message: ''};

 handleEvent(eventMessage: { message: string; page: string }) {
    // if there are any possibility to put somethings in eventMessage 
    if (eventMessage.message.includes('tablette')) {
      this.loadIframe('tablette');
    }

  }
}