12 } from '@angular/core'
15 export class DynamicElementService {
18 private injector: Injector,
19 private applicationRef: ApplicationRef
22 createElement <T> (ofComponent: Type<T>) {
23 const div = document.createElement('div')
25 const component = createComponent(ofComponent, {
26 environmentInjector: this.applicationRef.injector,
27 elementInjector: this.injector,
34 injectElement <T> (wrapper: HTMLElement, componentRef: ComponentRef<T>) {
35 const hostView = componentRef.hostView as EmbeddedViewRef<any>
37 this.applicationRef.attachView(hostView)
38 wrapper.appendChild(hostView.rootNodes[0])
41 setModel <T> (componentRef: ComponentRef<T>, attributes: Partial<T>) {
42 const changes: SimpleChanges = {}
44 for (const key of Object.keys(attributes)) {
45 const previousValue = componentRef.instance[key]
46 const newValue = attributes[key]
48 componentRef.instance[key] = newValue
49 changes[key] = new SimpleChange(previousValue, newValue, previousValue === undefined)
52 const component = componentRef.instance
53 if (typeof (component as unknown as OnChanges).ngOnChanges === 'function') {
54 (component as unknown as OnChanges).ngOnChanges(changes)
57 componentRef.changeDetectorRef.detectChanges()