Я сделал это ... с другим подходом
Я использовал DynamicComponentService
Важно : мне пришлось отключить «aot: false» в angular. json, иначе я получил Runtime compiler is not loaded
ошибок.
import {
Compiler,
Component,
ComponentFactory,
Injectable,
NgModule,
Type,
ViewContainerRef,
ViewEncapsulation
} from "@angular/core";
import {CommonModule} from "@angular/common";
@Injectable({
providedIn: "root"
})
export class DynamicComponentService {
protected cacheOfFactories: {[key: string]: ComponentFactory<any>};
protected componentCache: {[key: string]: Type<any>};
protected moduleCache: {[key: string]: Type<any>};
constructor(protected compiler: Compiler) {
this.cacheOfFactories = {};
this.componentCache = {};
this.moduleCache = {};
}
/**
*
* @param viewContainerRef
* @param selector
* @param template
*/
createComponentFactory(viewContainerRef: ViewContainerRef, selector: string, template: string) {
const componentFound = this.componentCache[selector];
if(componentFound) {
this.compiler.clearCacheFor(componentFound);
delete this.componentCache[selector];
}
const moduleFound = this.moduleCache[selector];
if(moduleFound) {
this.compiler.clearCacheFor(moduleFound);
delete this.moduleCache[selector];
}
viewContainerRef.clear();
this.componentCache[selector] = Component({
selector,
template,
encapsulation: ViewEncapsulation.None
})(class {
});
this.moduleCache[selector] = NgModule({
imports: [CommonModule],
declarations: [this.componentCache[selector]]
})(class {
});
return this.compiler.compileModuleAndAllComponentsAsync(this.moduleCache[selector])
.then((factories) => {
const foundFactory = factories.componentFactories.find((factory) => {
return factory.selector === selector;
});
if(foundFactory) {
return viewContainerRef.createComponent(foundFactory);
}
throw new Error("component not found");
})
.catch((error) => {
console.log("error", error);
this.compiler.clearCacheFor(componentFound);
delete this.componentCache[selector];
this.compiler.clearCacheFor(moduleFound);
delete this.moduleCache[selector];
return Promise.reject(error);
});
}
}
и изменил свой html -тип компонент на:
export class HtmlTypeComponent implements DoCheck {
@ViewChild('htmlcontrolcomponent', { read: ViewContainerRef }) entry: ViewContainerRef;
protected oldTemplate: string = "";
protected componentRef?: ComponentRef<any>;
constructor(private dynamicComponentService: DynamicComponentService) {}
ngDoCheck() {
if(this.entry && this.oldTemplate !== this.to.template) {
this.oldTemplate = this.to.template;
if(this.componentRef) {
this.componentRef.destroy();
delete this.componentRef;
}
this.entry.clear();
this.dynamicComponentService.createComponentFactory(this.entry, 'html-content', this.to.template)
.then((component) => {
this.componentRef = component;
this.componentRef.instance.model = this.model;
});
}
}
}
Я мог бы даже избавиться от Компонент HtmlControl