简体   繁体   English

Angular 6 - 路由器插座 - 使用 CSS 加载外部 HTML

[英]Angular 6 - router-outlet - Loading external HTML with CSS

I am trying to develop json based dynamic template using Angular 6.我正在尝试使用 Angular 6 开发基于 json 的动态模板。

There are some use cases where I would like to load external html (might be from DB as a text ) with css (as a separate file as a text from DB) and use this for the corresponding router-outlet html.在某些用例中,我想使用 css(作为单独的文件作为来自 DB 的文本)加载外部 html(可能来自 DB 作为文本)并将其用于相应的路由器出口 html。

For ex, We need to have a home.html and home.ts file for the below routing.例如,我们需要有一个 home.html 和 home.ts 文件用于以下路由。 Instead of having the html code inside home.html, I thought of loading from DB as a text and show the same.我没有将 html 代码放在 home.html 中,而是考虑从 DB 加载文本并显示相同内容。 Here, I would like to get the corresponding css code also from DB without hard-coding css code in html.在这里,我想也从 DB 获取相应的 css 代码,而无需在 html 中硬编码 css 代码。

Also, I need the corresponding JavaScript functions (say for dynamic html, i may have a delete link or cancel link) in a separate file.另外,我需要一个单独的文件中的相应 JavaScript 函数(比如动态 html,我可能有一个删除链接或取消链接)。 It would be similar to typescript.它类似于打字稿。 Can I load typescript file also dynamically?我也可以动态加载打字稿文件吗?

const routes: Routes = [
  { path: '', redirectTo: 'home', pathMatch: 'full' }
]

I can go with the below approach.我可以采用以下方法。

How do I load an HTML page in a <div> using JavaScript? 如何使用 JavaScript 在 <div> 中加载 HTML 页面?

But, trying to understand if there is any better approach for Angular 6.但是,试图了解 Angular 6 是否有更好的方法。

As I think the approach in the link is a good way of doing it.因为我认为链接中的方法是一种很好的方法。 Also you can use angular innerHtml property also.您也可以使用 angular innerHtml属性。

<div class="home" [innerHtml]="testhtml"></div>

Here testhtml is your html getting from db.这里testhtml是您从 db 获取的 html。 For more information take a look at this .有关更多信息,请查看

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

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