To simplify the process, consider utilizing a custom directive such as:
cell-template.directive.ts
import { Directive, Input, TemplateRef } from '@angular/core';
@Directive({
selector: 'ng-template[cellTemplate]'
})
export class CellTemplateDirective {
@Input('cellTemplate') name: string;
constructor(public template: TemplateRef<any>) {}
}
With this directive, you can specify templates for different types like:
<app-grid>
<ng-template let-rowData="rowData" cellTemplate="student">student {{rowData}}</ng-template>
<ng-template let-rowData="rowData" cellTemplate="manager">manager {{rowData}}</ng-template>
</app-grid>
Furthermore, ensure that your grid component is equipped to handle specific templates:
grid.component.ts
@ContentChildren(CellTemplateDirective) cellTemplates: QueryList<CellTemplateDirective>;
cellTemplatesMap: { [key: string]: TemplateRef<any> };
...
ngAfterContentInit() {
this.cellTemplatesMap = this.cellTemplates.reduce((acc, cur) => {
acc[cur.name] = cur.template;
return acc;
}, {});
}
The final step is to include the correct template in your HTML structure:
grid.component.html
<td *ngIf="column.type==='template'">
<ng-content *ngTemplateOutlet="cellTemplatesMap[column.columnName]; ...
View Forked Stackblitz Example