Angular 2将transcluded内容绑定到循环变量

Chr*_*odz 10 typescript angular

我试图将transcluded内容绑定到组件循环内部的变量,但我无法这样做.

我查看了PrimeNG的dropdown组件,他们一起使用template标签let-car绑定到car循环的变量.

但是,当我尝试这个时,我甚至无法将内容转换为内容.实现这一目标的正确方法是什么?

尝试:

<!-- Obviously not gonna work -->
<comp>
  <span>{{option.name}}, {{option.type}}</span>
</comp>

<!-- Thought this would work but it doesn't, in odd scenarios I get the last element of the loop to transclude content and bind correctly. -->
<comp>
  <template let-option>
    <span>{{option.name}}, {{option.type}}</span>
  </template>
</comp>
Run Code Online (Sandbox Code Playgroud)

在组件中:

<ul>
  <li *ngFor="let option of options">
    <ng-content></ng-content>
  </li>
</ul>
Run Code Online (Sandbox Code Playgroud)

简单的我正在努力实现的目标:

https://plnkr.co/edit/6SS03fuXmbvJB4nmf1AO?p=preview

Gün*_*uer 12

更新Angular 6

ngOutletContext更名为ngTemplateOutletContext templateng-template

另见https://github.com/angular/angular/blob/master/CHANGELOG.md#500-beta5-2017-08-29

原版的

您可以获取模板引用并使用例如添加它,ngTemplateOutlet或者ngForTemplate将模板内容添加到DOM中.随着ngTemplateOutlet您可以提供自己的背景,你可以接着用模板的变量,比如你试图访问.

class CompComponent {
  context = {option: 'some option'};
  constructor(private templateRef:TemplateRef){}
}
Run Code Online (Sandbox Code Playgroud)
<ng-template [ngTemplateOutlet]="templateRef" [ngOutletContext]="{$implicit: context}"></template>
Run Code Online (Sandbox Code Playgroud)

我认为在新版本中要这样做

<ng-template [ngTemplateOutlet]="templateRef" [ngTemplateOutletContext]="{$implicit: context}"></template>
Run Code Online (Sandbox Code Playgroud)

(但尚未验证)

也可以看看