Skip to content

Instantly share code, notes, and snippets.

@ssougnez
Last active October 9, 2021 19:49
Show Gist options
  • Save ssougnez/00da05f45aa1c556547fc69fb6f90e31 to your computer and use it in GitHub Desktop.
Save ssougnez/00da05f45aa1c556547fc69fb6f90e31 to your computer and use it in GitHub Desktop.
import {
AfterViewInit,
Component,
ComponentFactoryResolver,
Input,
ViewChild,
ViewContainerRef,
} from '@angular/core';
import { BaseHistoryEntryComponent } from './base-history-entry.component';
import { GetHistoryEntryComponent } from './history-entry-type.decorator';
import { HistoryEntry } from './history-entry.model';
@Component({
selector: 'generic-history-entry',
template: `<ng-template #host></ng-template>`,
})
export class GenericHistoryEntryComponent implements AfterViewInit {
@Input()
public entry: HistoryEntry;
@ViewChild('host', { read: ViewContainerRef })
public hostRef: ViewContainerRef;
constructor(private _cfr: ComponentFactoryResolver) {}
public ngAfterViewInit() {
const componentType = GetHistoryEntryComponent(this.entry.type);
const factory = this._cfr.resolveComponentFactory(componentType);
const component = this.hostRef.createComponent(factory);
const instance = component.instance as BaseHistoryEntryComponent<any>;
instance.data = this.entry.data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment