我正在尝试将CS​​S文件注入需要更改样式的第三方iframe。

我想到了

ngOnInit() {
...

this.myIframe.nativeElement.document.head.appendChild('style.css')

}


但由于某些原因,它无法正常工作。

任何帮助将不胜感激。

干杯

最佳答案

如果您的Iframe不是来自其他域,则此解决方法将起作用。您将收到跨浏览器错误。
模板

<iframe #myIframe></iframe>


零件

@ViewChild('myIframe') public myIframe;
..........
ngAfterViewInit() {
  const iframDoc = this.myIframe.nativeElement.contentWindow.document;
  .....
  iframDoc.head.appendChild('style.css');
}
.....

09-27 15:15