I’m trying to enhance an angular table component I created to output formatted cell content instead of just text. I thought a set of format components would be the way to go as they will have to contain html tags (which would be stripped by simply binding to a property). I guess pipes might have been an option but as most of the functionality is in a base component I don't want much in the way of specific code in instances of the table.
So I tried the Angular document on Dynamic Component Loading and created a [format-host] directive, linked it all up as in the doc & tried to debug. The failure is in the component that uses the directive.
let viewContainerRef = this.formatHost.viewContainerRef;
this fails as formatHost is undefined. The directive is defined in the module.
I'm using the VS2017 angular SPA template, angular 4.2.5 The relevant code in the using component is (kept it short):
export class PagedTableComponent implements OnInit, AfterViewInit{
@Input() pagedTable: PagedTable<any[]>;
@Output() tableState: EventEmitter<TableState> = new EventEmitter<TableState>();
…
@ViewChild(FormatDirective) formatHost: FormatDirective;
…
formatters: Formatter[] = [];
constructor(private componentFactoryResolver: ComponentFactoryResolver) {
}
loadFormatComponent() {
let componentFactory = this.componentFactoryResolver.resolveComponentFactory(this.formatters[0].component);
let viewContainerRef = this.formatHost.viewContainerRef;
viewContainerRef.clear(); //??
let componentRef = viewContainerRef.createComponent(componentFactory);
}
ngOnInit() {
…
//this.formatters.push(new Formatter(ContactFormatComponent, "test"));
// this.loadFormatComponent();
}
ngAfterViewInit() {
this.formatters.push(new Formatter(ContactFormatComponent, "test"));
this.loadFormatComponent();
}
Any idea on why the directive is undefined would be appreciated.
directive:
import { Directive, ViewContainerRef } from '@angular/core';
@Directive({
selector: '[format-host]',
})
export class FormatDirective {
constructor(public viewContainerRef: ViewContainerRef) { }
}
template:
<td *ngFor="let column of pagedTable.tableConfig.columns"
[ngClass]="{ 'cell-right' : column.justification === ColumnJustification.right, 'cell-center' : column.justification === ColumnJustification.center}">
....
<ng-template format-host></ng-template>
</td>