import { Component, Input, ComponentFactoryResolver, ViewChild, ViewContainerRef, OnInit, OnDestroy, } from '@angular/core'; import { Cell } from '../../../lib/data-set/cell'; import { ViewCell } from './view-cell'; @Component({ selector: 'custom-view-component', template: ` `, }) export class CustomViewComponent implements OnInit, OnDestroy { customComponent: any; @Input() cell: Cell; @ViewChild('dynamicTarget', { read: ViewContainerRef }) dynamicTarget: any; constructor(private resolver: ComponentFactoryResolver) { } ngOnInit() { if (this.cell && !this.customComponent) { this.createCustomComponent(); this.callOnComponentInit(); this.patchInstance(); } } ngOnDestroy() { if (this.customComponent) { this.customComponent.destroy(); } } protected createCustomComponent() { const componentFactory = this.resolver.resolveComponentFactory(this.cell.getColumn().renderComponent); this.customComponent = this.dynamicTarget.createComponent(componentFactory); } protected callOnComponentInit() { const onComponentInitFunction = this.cell.getColumn().getOnComponentInitFunction(); onComponentInitFunction && onComponentInitFunction(this.customComponent.instance); } protected patchInstance() { Object.assign(this.customComponent.instance, this.getPatch()); } protected getPatch(): ViewCell { return { value: this.cell.getValue(), rowData: this.cell.getRow().getData() } } }