首页 > 解决方案 > 在 angular html 之间从 assets 文件夹中添加 html

问题描述

我想从我的 Angular 的 html 文件之间的资产文件夹中加载 html。例如:假设这是我的产品组件的 product.html,detailPara.html 和 BlockPara.html 在 assets 文件夹中,我必须在 product.html 之间添加:

<div class="productPage">
....
<div innerHTML="../assets/product/detailPara.html"></div>
....
<div innerHTML="../assets/product/BlockPara.html"></div>
</div>

有人可以帮帮我吗。

标签: angular

解决方案


你可以像这样使用它。

let path = 'assets/' + {fileName} + '.html';
this._httpClient.get(path, {responseType: "text"}).subscribe(
data => {
//now you have the file content in 'data'
this.content = this.sanitizer.bypassSecurityTrustHtml(data);
});

只需在构造函数中注入 DOMSanitizer 就可以了!

然后在您的 HTML 文件中。

<div class="productPage">
....
<div [innerHTML]="content"></div>
....

推荐阅读