Angular 会忽略其模板中的 script
标签,但它们是加载 GitHub gist 所必需的。这样做的最佳做法是什么?使用 iframe
?动态创建 script
标签?还是别的?
最佳答案
一种方法是创建一个包含 script
的 iframe
并在您希望要点出现时附加它(基于 jasonhodges 解决方案)。
模板
<iframe #iframe type="text/javascript"></iframe>
组件
@ViewChild('iframe') iframe: ElementRef;
gistUrl: string;
ngAfterViewInit() {
const doc = this.iframe.nativeElement.contentDocument || this.iframe.nativeElement.contentElement.contentWindow;
const content = `
<html>
<head>
<base target="_parent">
</head>
<body>
<script type="text/javascript" src="${this.gistUrl}"></script>
</body>
</html>
`;
doc.open();
doc.write(content);
doc.close();
}
关于javascript - 如何在 Angular 模板中嵌入 GitHub 要点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50969506/