Created
April 28, 2017 14:49
-
-
Save benjamincharity/8116414c7f38cffe3cef0e44fe44295d to your computer and use it in GitHub Desktop.
A directive to dynamically compile HTML. Source: https://plnkr.co/edit/mcvILwmOLvrS2PxIrXX8?p=preview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
Component, | |
Directive, | |
NgModule, | |
Input, | |
ViewContainerRef, | |
Compiler, | |
ComponentFactory, | |
ModuleWithComponentFactories, | |
ComponentRef, | |
ReflectiveInjector | |
} from '@angular/core'; | |
import { RouterModule } from '@angular/router'; | |
import { CommonModule } from '@angular/common'; | |
export function createComponentFactory(compiler: Compiler, metadata: Component): Promise<ComponentFactory<any>> { | |
const cmpClass = class DynamicComponent {}; | |
const decoratedCmp = Component(metadata)(cmpClass); | |
@NgModule({ imports: [CommonModule, RouterModule], declarations: [decoratedCmp] }) | |
class DynamicHtmlModule { } | |
return compiler.compileModuleAndAllComponentsAsync(DynamicHtmlModule) | |
.then((moduleWithComponentFactory: ModuleWithComponentFactories<any>) => { | |
return moduleWithComponentFactory.componentFactories | |
.find(x => x.componentType === decoratedCmp); | |
}); | |
} | |
@Directive({ | |
selector: 'client-html-outlet', | |
}) | |
export class ClientHtmlOutletDirective { | |
@Input() html: string; | |
cmpRef: ComponentRef<any>; | |
constructor(private vcRef: ViewContainerRef, private compiler: Compiler) { } | |
ngOnChanges() { | |
const html = this.html; | |
if (!html) return; | |
if (this.cmpRef) { | |
this.cmpRef.destroy(); | |
} | |
const compMetadata = new Component({ | |
selector: 'dynamic-html', | |
template: this.html, | |
}); | |
createComponentFactory(this.compiler, compMetadata).then(factory => { | |
const injector = ReflectiveInjector.fromResolvedProviders([], this.vcRef.parentInjector); | |
this.cmpRef = this.vcRef.createComponent(factory, 0, injector, []); | |
}); | |
} | |
ngOnDestroy() { | |
if (this.cmpRef) { | |
this.cmpRef.destroy(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm trying to use your directive to include a mvc partial view in my angular. But it doesn't work with Angular 10. Any Idea what's wrong?