如何使用Angular 6从iframe获取数据

时间:2018-09-04 15:49:32

标签: javascript angular typescript events iframe

我有一些iframe:

<iframe src="/node_modules/collaborator-gallery/dist/index.html?id=3823&amp;auto_play=true&amp;track=true&amp;initialWidth=1290&amp;childId=pymd-aaeafe70-910d-494b-ac6b-e68d8eb2de66&amp;parentUrl=http%3A%2F%2Flocalhost%3A8080%2Fresources%2Fexecute%2F20895"
width="100%" scrolling="no" marginheight="0" frameborder="0" class="embed-responsive-item" height="480px"></iframe>

通过它我使用图库。 如何通过Angular 6中的另一个组件获取事件

1 个答案:

答案 0 :(得分:0)

在窗口A的脚本中,其中A在http://example.com:8080上:

`var popup = window.open(...popup details...);
popup.postMessage("The user is 'bob' and the password is 'secret'", "https://secure.example.net");
popup.postMessage("hello there!", "http://example.com");`

`function receiveMessage(event) {
  if (event.origin !== "http://example.com")
    return;
  // event.data is "hi there yourself!  the secret response is: rheeeeet!"
}`
`window.addEventListener("message", receiveMessage, false);`

在调用postMessage后的某个时间调用

`

function receiveMessage(event) {
  if (event.origin !== "http://example.com:8080")
    return;
  // event.data is "hello there!"
  event.source.postMessage("hi there yourself!  the secret response " + "is: rheeeeet!", event.origin);
}`

window.addEventListener("message", receiveMessage, false);