javascript - 如何在 Angular 模板中嵌入 GitHub 要点?
问题描述
Angular 会忽略script
其模板中的标签,但它们是加载 GitHub gist 所必需的。这样做的最佳做法是什么?使用iframe
? 动态创建script
标签?或者是其他东西?
解决方案
一种方法是在内部创建一个iframe
withscript
并在您希望您的要点出现时附加它(基于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();
}
推荐阅读
- xml - 如何在 XSLT 中聚合值?
- reactjs - TestCafé + React JSX 错误(意外的令牌)
- azure - 查询 Azure 云服务暂存槽是否预配了 VM
- c# - .editorconfig 错误/警告仅显示打开的文件
- python - 在树莓派 3 上为 docker 安装 OpenCV
- python - Sympy 集成错误:无法将符号转换为 int
- python - 如何处理多个功能的过滤器?
- python - 如何使用 baggingregressor 进行特征选择?
- python - 如何根据python中的地图将rgb值转换为整数
- c++ - 带有地址但不引用地址的 C++ 指针