I am trying to build a listing component in Angular2 that takes the items, the columns and the templates for the fields of the items from the user of the component. So I am trying to use ngTemplateOutlet
and ngOutletContext
(which I have read are experimental). But I cannot get it to work.
Here is a simplified component to demonstrate what I am trying to do:
<div *ngFor="let item of items>
<span *ngFor="let column of columns>
<template [ngOutletContext]="{ item: item }"
[ngTemplateOutlet]="column.templateRef"></template>
</span>
</div>
Here is the usage of the component:
<my-component [items]="cars" [columns]="carColumns">
<template #model>{{item.model}}</template>
<template #color>{{item.color}}</template>
<template #gearbox>{{item.gearbox}}</template>
</my-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 = [
{ templateRef: "model" },
{ templateRef: "color" },
{ templateRef: "gearbox" }
];
Here is a plunker reproducing the issue after adapting the code according to Günters comment: https://plnkr.co/edit/jB6ueHyEKOjpFZjxpWEv?p=preview
TemplateRef
as string? One way to do it plnkr.co/edit/3yM25cUVeP4fK0cxhpXp?p=preview – IntuitiveContentChildren
plnkr.co/edit/2Ohj12Gax6UaK6LJ5dwD?p=preview – IntuitivengTemplateOutlet
param should beTemplateRef
instance but in your case it'sstring
– Intuitive<template #model>
model
will be TemplateRef instance. If you want to usemodel
as TemplateRef in your component then you should look atViewChild
(ViewChildren
) #39909467 See also angular.io/docs/ts/latest/guide/template-syntax.html#!#ref-vars – Intuitive