当HTML来自数据库时,如何在angular 6中呈现gist代码(GItHub)



我将HTML页面存储在MongoDb中,并在运行时基于blogId将其呈现为innerHTML。我使用gridfs将HTML代码存储在Mongodb中。我的示例文件看起来像

<p>Hello world, i am coming from database </p>        
<gh-gist src="https://gist.github.com/user/0f7c1a14489ecf9e98b70ea4e276fb.js"></gh-gist>
<p> Page code continue after</p>

现在的问题是页面被正确地呈现,但是GIST的代码没有被呈现和显示。

但是,如果没有存储在数据库中,相同的代码可以很好地显示预期的GIST代码和HTML。

没有显示错误,只是代码没有出现。

我尝试了下面提供的建议-Angular 2:清理HTML剥离了css样式上的一些内容,但没有帮助。

我的页面HTML代码类似于-

<div [innerHTML]="pageContent | noSanitize"></div>

以下是建议定义的管道代码-

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
@Pipe({ name: 'noSanitize' })
export class NoSanitizePipe implements PipeTransform {
constructor(private domSanitizer: DomSanitizer) {
}
transform(html: string): SafeHtml {
return this.domSanitizer.bypassSecurityTrustHtml(html);
}
}

我正在使用https://www.npmjs.com/package/@sgbj/angular gist,用于在HTML中嵌入gist。

解决方案-我使用ng dynamic解决了这个问题。

最新问题-但现在我在使用webpack进行生产构建时遇到了问题。由于ng dynamic使用JITCompiler,webpack使用--aot编译器。因此ng dynamic不适用于--aot构建。

错误我现在得到-

An unexpected error occured :Error: Uncaught (in promise): TypeError: t is not a constructor
TypeError: t is not a constructor
at http://localhost:4200/main.c07d94d02c96a651ccd3.js:1:165321
at No (http://localhost:4200/main.c07d94d02c96a651ccd3.js:1:165568)

感谢您的帮助。

我有完全相同的错误

发生意外错误:错误:未捕获(在承诺中(:类型错误:t不是构造函数TypeError:t不是构造函数

切换到使用ngx-gist

它在产品中运行良好

https://github.com/jasonhodges/ngx-gist

最新更新