12 } from '@angular/core'
13 import { objectKeysTyped } from '@shared/core-utils'
16 export class DynamicElementService {
19 private injector: Injector,
20 private applicationRef: ApplicationRef
23 createElement <T> (ofComponent: Type<T>) {
24 const div = document.createElement('div')
26 const component = createComponent(ofComponent, {
27 environmentInjector: this.applicationRef.injector,
28 elementInjector: this.injector,
35 injectElement <T> (wrapper: HTMLElement, componentRef: ComponentRef<T>) {
36 const hostView = componentRef.hostView as EmbeddedViewRef<any>
38 this.applicationRef.attachView(hostView)
39 wrapper.appendChild(hostView.rootNodes[0])
42 setModel <T> (componentRef: ComponentRef<T>, attributes: Partial<T>) {
43 const changes: SimpleChanges = {}
45 for (const key of objectKeysTyped(attributes)) {
46 const previousValue = componentRef.instance[key]
47 const newValue = attributes[key]
49 componentRef.instance[key] = newValue
50 changes[key as string] = new SimpleChange(previousValue, newValue, previousValue === undefined)
53 const component = componentRef.instance
54 if (typeof (component as unknown as OnChanges).ngOnChanges === 'function') {
55 (component as unknown as OnChanges).ngOnChanges(changes)
58 componentRef.changeDetectorRef.detectChanges()