关于angular:无法让ngTemplateOutlet工作

Can't get ngTemplateOutlet to work

我试图在Angular2中构建一个列表组件,该组件从组件的用户那里获取项目,项目的列和字段的模板。 所以我正在尝试使用ngTemplateOutletngOutletContext(我读过的是实验性的)。 但是我无法使其正常工作。

这是一个简化的组件来演示我正在尝试做的事情:

1
2
3
4
  <span *ngFor="let column of columns>
    <template [ngOutletContext]="{ item: item }"
              [ngTemplateOutlet]="column.templateRef"></template>
  </span>

这是组件的用法:

1
2
3
4
5
<my-component [items]="cars" [columns]="carColumns">
  <template #model>{{item.model}}</template>
  <template #color>{{item.color}}</template>
  <template #gearbox>{{item.gearbox}}</template>
</my-component>

这是示例数据:

1
2
3
4
5
6
7
8
9
10
11
12
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" }
];

这是一个根据Günters评论修改代码后重现问题的小伙子:
https://plnkr.co/edit/jB6ueHyEKOjpFZjxpWEv?p=preview