EDIT: I found [ngTemplateOutlet]
which seems like a much better fit. Discussion continued in Can't get ngTemplateOutlet to work
ORIGINAL QUESTION:
I have built a table component that I will use in multiple different views to present different data. That data and it's presentation should be controlled by the consumer of the table component.
I tried to use <ng-content>
to pass along a "renderer" that knows how to retrieve that data and how it should be presented. As you might understand, it was illegal use.
Here is a simplification of how I tried to use <ng-content>
.
This is the table component template:
<table>
<thead>
<tr>
<th *ngFor="let column in columns">{{column.heading}}</th>
<tr>
</thead>
<tbody>
<tr *ngFor="let item of items>
<td *ngFor="let column of columns>
<ng-content select="{{column.template}}"></ng-content>
</td>
</tr>
</tbody>
</table>
Here is the usage of the table component:
<my-table-component [items]="cars" [columns]="carColumns">
<div class="model">{{item.model}}</div>
<div class="color">{{item.color}}</div>
<div class="gearbox>{{item.gearbox}}</div>
</my-table-component>
And here is example data:
cars = [
{ model: "volvo", color: "blue", gearbox: "manual" },
{ model: "volvo", color: "yellow", gearbox: "manual" },
{ model: "ford", color: "blue", gearbox: "automatic" },
{ model: "mercedes", color: "silver", gearbox: "automatic" }
];
carColumns = [
{ heading: "Model", template: ".model" },
{ heading: "Color", template: ".color" },
{ heading: "Gear Box", template: ".gearbox" }
];
By this I hope that you understand what I am trying to do. The obvious issues seems to be that <ng-content>
does not work within a "loop" and it will not accept a dynamic "select". Also, item
is of course not selectable it the table component consumer.
Most likely there are additional "do not".
As I am new to angular 2 I am hoping that I am mistaking something, or that I am using the wrong tools. Any ideas?