I have (which I think would be a fairly common problem) that I cannot find a good way to solve with the current Angular 4.x architecture. Maybe there is a method that I have not found yet, but I searched quite widely.
I would like to add dynamic user-created HTML content to an Angular app. This HTML content also includes some well-known (included in the module declaration) Angular components that should be displayed. Below are some puesdo-app HTML that will help me explain:
<app-component>
<app-header>
<app-nav>
<ul>
<li routerLink="/page-one">First Page</li>
<li routerLink="/page-two">Second Page</li>
<li routerLink="/page-three">Third Page</li>
</ul>
</app-nav>
</app-header>
<router-outlet></router-outlet>
<main>
<h1>Title</h1>
<app-component>
<p>this component and its content should be initalized when HTML is inserted into main</p>
</app-component>
<app-component>
<app-sub-component>
angular components here might also contain other angular components
</app-sub-component>
</app-component>
<p>more regular HTML content</p>
</main>
<app-footer></app-footer>
</app-component>
I tried two methods to achieve this, none of which really work.
1. Using the Dynamic Template / Component with a JIT compiler.
TL;DR; AOT. AOT - .
, . . JIT-, HTML . , AOT. Angular 4.x JitCompilerFactory AOT. , JIT Complier AOT, JIT- . . , Angular, , JIT-.
2.
TL;DR; .
JIT- AOT , ComponentFactoryResolver. , , , HTML-. , , . :
- HTML-
- HTML
- Angular
DomSanatizer.bypassSecurityTrustHtml(), - DOM
- HTML
{<app-selector-string> : ComponentClass} json- DOM ComponentClass
ViewContainerRef , ( )ViewContainerRef.createComponent(),- , .
, ViewContainerRef 3. ( - 3. IDK. )
? ?
!