简体   繁体   English

从服务器将HTML(带有angular 2指令)插入DOM

[英]Inserting HTML (with angular 2 directives) from the server into the DOM

I have ~2,000 lines of HTML code that I can retrieve from my Java server. 我有大约2,000行HTML代码,可以从Java服务器中检索。 This HTML code also has angular directives inside of it, such as ngIf* and (click) and [ngClass] . 该HTML代码中还包含有角度指令,例如ngIf*(click)[ngClass] When I try to display that HTML in my client with such methods as the below: 当我尝试使用以下方法在客户端中显示该HTML时:

<div [innerHTML]="htmlData"></div>
<div innerHTML="{{ htmlData }}"></div>

I see all of the HTML elements displayed correctly, but none of the angular directives are working (such as some of the HTML should be hidden with the *ngIf but they aren't... and none of the (click) events work. What can I do to fix this? 我看到所有HTML元素都能正确显示,但是所有的angular指令都没有用(例如,某些HTML应该用*ngIf隐藏,但它们不能...并且所有(click)事件都没有用。我该怎么做才能解决此问题?

I need this functionality because I am delivering my code as a JAR file to various users to run on their local machines, and I want to serve the HTML code from some Amazon's S3 so I can update it whenever I want to give their website an update. 我需要此功能,因为我将代码作为JAR文件交付给各种用户以在其本地计算机上运行,​​并且我希望从某些Amazon S3提供HTML代码,以便在我希望对其网站进行更新时可以对其进行更新。 。

And I tried looking at the below answers, but those dealt with pure HTML from the server, and didn't have any angular directives. 我尝试着看下面的答案,但是那些答案是来自服务器的纯HTML,并且没有任何角度指令。

Inserting HTML from server into DOM with angular2 (general DOM manipulation in Angular2) 使用angular2将服务器中的HTML插入DOM(Angular2中的一般DOM操作)

Angular HTML binding 角度HTML绑定

Unfortunately it's not possible. 不幸的是,这是不可能的。 InnterHTML will put HTML code there - not angular. InnterHTML会将HTML代码放在此处-不成角度。 Angular code should be compiled ahead of time - a process where all the directives and html magic is replaced with simple JS. Angular代码应提前编译-在此过程中,所有指令和html magic都将替换为简单的JS。

You can transform you angular code to plain html by server side rendering with node js. 您可以使用node js通过服务器端渲染将角度代码转换为纯HTML。

You can read more about server side rendering here: https://angular.io/guide/universal 您可以在此处阅读有关服务器端渲染的更多信息: https : //angular.io/guide/universal

But just as well you can use JAVA and with any popular templating framework for JAVA servers if you'll send your parameters on the http request. 但是,如果您要在http请求上发送参数,则同样可以使用JAVA以及任何流行的JAVA服务器模板框架。

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

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