Javascript Angular:如何在Angular 5中实现ng include-like功能?

Javascript Angular:如何在Angular 5中实现ng include-like功能?,javascript,html,angular,angularjs-ng-include,angular5,Javascript,Html,Angular,Angularjs Ng Include,Angular5,是否有基于路径在组件模板内动态呈现html文件的功能?我有了本地html文件的路径,现在我需要在组件模板中显示它的内容。在Anular 1.x中,我们有ng include,Angular5中有类似的功能吗 <div class="row blogdetail-container"> {{blogSelected.description}} // currently interpolates the path </div> {{blogSelected

是否有基于路径在组件模板内动态呈现html文件的功能?我有了本地html文件的路径,现在我需要在组件模板中显示它的内容。在Anular 1.x中,我们有ng include,Angular5中有类似的功能吗

<div class="row blogdetail-container">
        {{blogSelected.description}} // currently interpolates the path
</div>

{{blogSelected.description}}//当前插入路径

变量
blogSelected.description
包含html文件的路径,我想在这里替换它的内容。

好的,所以我能想到的唯一简单方法是使用http请求获取html文件的内容,然后在
[innerHtml]
属性中使用它

private dynamicTemplate:any=”“;
http.get(blogSelected.description).map((html:any)=>this.dynamicTemplate=sanitizer.sanitize(html))

然后使用

<div class="row blogdetail-container" [innerHtml]="dynamicTemplate"></div>

注意1:记住将http作为此组件的依赖项

注意2:记住要将消毒剂作为此组件的依赖项


注意3:在调用http请求以检查其是否为有效URL之前,请记住验证
blogSelected.description

这是不可能的,但您可以在此处使用自定义管道尝试查看此blogSelected.description是组件级属性,无法在导入中访问。如果是角度,在将html传递给DOM之前,您可能还需要对其进行清理<代码>sanitizer.sanitize(SecurityContext.html,dynamicTemplate)
@vincecampanale Good shout;我将更新答案:)@VishalGulati您有机会尝试一下吗?@VishalGulati Angular 5中没有相似的功能。有一个相当长的讨论。TLDR:它会阻止脱机编译,并且存在安全风险