How To Embed A Github Gist In The Angular Template?
Angular ignores script tags in its template, but they are required to load GitHub gist. What is the best practice to do this? Using iframe? Creating script tag dynamically? Or some
Solution 1:
One method is to create an iframe
with script
inside and append it whenever you want your gist to appear (based on jasonhodges solution).
Template
<iframe #iframe type="text/javascript"></iframe>
Component
@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();
}
Post a Comment for "How To Embed A Github Gist In The Angular Template?"