In my Angular 5 project I need to create one component dynamically after API data is downloaded (I have HTML code inside with Angular markups - I need to compile them after request is done), but I see there is the problem with AOT.
I've tried to use solution from that topic Angular 4 | Template bind through innerHTML, component variables are not accessible (damContentRoot is this case) (and from that How can I use/create dynamic template to compile dynamic Component with Angular 2.0?)
Here is my current code:
import {
Directive,
OnChanges,
Input,
ComponentRef,
ViewContainerRef,
Compiler,
ModuleWithComponentFactories,
Component,
NgModule,
Type
} from '@angular/core';
import {CommonModule} from '@angular/common';
import { FormsModule, ReactiveFormsModule, NgForm } from '@angular/forms';
import {JitCompilerFactory} from '@angular/platform-browser-dynamic';
@Directive({selector: '[compile]'})
export class CompileDirective implements OnChanges {
@Input()compile : string;
@Input()compileContext : any;
compRef : ComponentRef < any >;
constructor(private vcRef : ViewContainerRef, private compiler : Compiler) {}
ngOnChanges() {
if (!this.compile) {
if (this.compRef) {
this.updateProperties();
return;
}
throw Error('You forgot to provide template');
}
this
.vcRef
.clear();
this.compRef = null;
const component = this.createDynamicComponent(this.compile);
const module = this.createDynamicModule(component);
this
.compiler
.compileModuleAndAllComponentsAsync(module)
.then((moduleWithFactories : ModuleWithComponentFactories < any >) => {
let compFactory = moduleWithFactories
.componentFactories
.find(x => x.componentType === component);
this.compRef = this
.vcRef
.createComponent(compFactory);
this.updateProperties();
})
.catch(error => {
console.log(error);
});
}
updateProperties() {
for (var prop in this.compileContext) {
this.compRef.instance[prop] = this.compileContext[prop];
}
}
private createDynamicComponent(template : string) {
@Component({selector: 'custom-dynamic-component', template: template})
class CustomDynamicComponent {}
return CustomDynamicComponent;
}
private createDynamicModule(component : Type < any >) {
@NgModule({
// You might need other modules, providers, etc... Note that whatever components
// you want to be able to render dynamically must be known to this module
imports: [CommonModule, FormsModule, ReactiveFormsModule],
declarations: [component],
})
class DynamicModule {}
return DynamicModule;
}
}
<ng-container *compile="this.pageData.html; context: this"></ng-container>
But I'm getting error :
ERROR Error: Runtime compiler is not loaded
I've tried to use JitCompilterFactory also but I'm getting error like there : https://github.com/angular/angular/issues/20639.
Is there any way to to add component at runtime with aot compilation ? Or it's not making any sens to combine it together with JIT?
I've read whole topic about dynamic component loading (https://blog.angularindepth.com/here-is-what-you-need-to-know-about-dynamic-components-in-angular-ac1e96167f9e) and I still can't find solution, always JitCompilerFactory
error.