简体   繁体   English

Angular 7+:在运行时从REST API获取的动态用户内容(HTML)中的渲染指令

[英]Angular 7+: Rendering directives in dynamic user content (HTML) fetched from REST API in runtime

I have an app which fetches user-content like blog content, which is saved as HTML on the server. 我有一个可获取用户内容(如博客内容)的应用程序,该内容以HTML格式保存在服务器上。

I need to render it and it might have custom directives like 我需要渲染它,它可能具有类似的自定义指令

REST API RESPONSE SAMPLE:
content: "<div>
<span class="impressive-css-class">This is rendered perfectly by passing HTML to sanitizer.bypassSecurityTrustHtml</span>
and 
<span spoiler>SpiderMan dies on Page 142: Rendered as HTML. Directive not processed (naturally)</span>
</div>"

I need to render the spoiler directive on this dynamically returned user-content. 我需要在此动态返回的用户内容上呈现spoiler指令。

I have come to know from SO and extensive searching that one way to do it is by enabling JIT (no AOT) during the build process (buildOptimizer: false, and import 'core-js/es7/reflect' in polyfills.ts, create module/component dynamically using JIT) in a long complicated way (which might break on angular version upgrades) 我从SO和广泛的搜索中了解到,一种方法是通过在构建过程中启用JIT(无AOT)(buildOptimizer:false,然后在polyfills.ts中导入“ core-js / es7 / reflect”,创建长时间复杂的方式(使用JIT动态地使用模块/组件)(可能会在角度版本升级时中断)

Is there any other way to render my use-case dynamically that I'm unaware of ? 还有其他我不知道的动态呈现用例的方法吗?

Possible ways to implement this 可能的实现方式

  • Enabling JIT compiler and creating dynamic modules 启用JIT编译器并创建动态模块
  • Angular Elements 角度元素
  • Modifying the rendered HTML using native JS or some other library 使用本机JS或其他库修改呈现的HTML

You can use: 您可以使用:

<div [innerHTML]="content"></div>

Which will render your content variable inside that element 这将使您的内容变量在该元素内

However, thats not the purpose of a REST api. 但是,那并不是REST API的目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM