iframe - 更改 Angular 2中iframe内元素的属性

标签 iframe angular

我想在 Angular 2 Typescript 中更改 iframe 内元素的属性,类似于 JavaScript 代码

document.getElementById('iframeId').window.document.getElementById('home-grid').style.visibility = "隐藏";

我的 Angular typescript 代码:

var iframe   = document.getElementById('iframeId');
var insideDoc = iframe.contentDocument || iframe.contentWindow.document;

编译代码时出错:

stream.js:74
      throw er; // Unhandled stream error in pipe.
      ^
 Error: ./angularapp/web/component/mainPage/mainPage.ts
←[37m(←[39m←[36m35←[39m,←[36m32←[39m): ←[31merror TS2339: Property 'contentDocument' does not exist on type 'HTMLElement'.←[39m./angularapp/web/component/mainPage/mainPage.ts
←[37m(←[39m←[36m35←[39m,←[36m58←[39m): ←[31merror TS2339: Property 'contentWindow' does not exist on type 'HTMLElement'.←[39m

有什么办法可以在 Angular 2 中实现这一点吗?请帮忙

最佳答案

@Component({
  selector: 'my-app',
  template:`
    <h1>Selecting Number</h1>
    <iframe id="iframeId" src="iframe.html" (load)="onLoad()"></iframe>
  `,
})
export class App {
  onLoad()  {
    var iframe   = document.getElementById('iframeId');
    var iWindow = iframe.contentWindow
    var doc = iframe.contentDocument || iframe.contentWindow.document;
    console.debug(doc);
    console.log(doc.getElementById('foo').innerText);
  }
}

Plunker demo

关于iframe - 更改 Angular 2中iframe内元素的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36513929/

相关文章:

html - 将 IFrame 的高度设置为 100% 不会导致它扩展以填充页面吗?

html - 如何动态选择 Angular 5的下拉列表?

python - Angular 节点与 docker 内的 python 后端通信失败

javascript - C#.NET : Server is not able to validate Angular Client [IdentityServer]

angular - 当 token 在 Angular 4中过期时如何重定向到注销

asp.net - 如何从 C# 代码隐藏设置 iframe 的最小高度?

wordpress - Disqus iframe 透明度不适用于 Chrome 87

javascript - 可以跨 iframe 共享 javascript 导入吗?

javascript - 确定 iframe 是否在屏幕上可见

angular - 命令 'ng-build' 上没有这样的文件或目录错误