Templates Angular 2中的列表项渲染器

Templates Angular 2中的列表项渲染器,templates,angular,Templates,Angular,我得到了一个“哑”列表组件,它使用另一个“哑”组件来呈现单个项目。当我想将数据传递给渲染器时,我的列表组件中必须有相同的属性。例如,如果我想为项设置“showTimestamp”布尔属性,我必须这样做: list.template.html <my-item-renderer *ngFor="let item of items" [showTimestamp]="showTimestamp"></my-item-renderer> 有没有办法替换组件模板的一部分 我

我得到了一个“哑”列表组件,它使用另一个“哑”组件来呈现单个项目。当我想将数据传递给渲染器时,我的列表组件中必须有相同的属性。例如,如果我想为项设置“showTimestamp”布尔属性,我必须这样做:

list.template.html

<my-item-renderer *ngFor="let item of items" [showTimestamp]="showTimestamp"></my-item-renderer>

有没有办法替换组件模板的一部分

我想这样做:

<my-list [items]="items"><ng-content><item-renderer [showTimestamp]="true"></item-renderer></ng-content></my-list>


因此,my list get just items属性和in ng content将获得一个已设置showTimestamp变量的呈现程序。

您可以使用此模板:

<my-list [items]="items">
  <template><item-renderer [showTimestamp]="true"></item-renderer></template></my-list>

@组件({
选择器:“我的列表”,
模板:`
`
})
类MyList{
@输入()数据;
构造函数(私有templateRef:templateRef){}
}
另见


使用代码时,我在运行时遇到“没有TemplateRef的提供程序”错误。第二和第三个链接使用
@ContentChild(TemplateRef)TemplateRef:TemplateRef而不是构造函数注入。你能试试那个吗。有段时间我自己用过了,谢谢<代码>@ContentChild(TemplateRef)TemplateRef:TemplateRef正在工作。
@Component({
  selector: 'my-list',
  template: `
      <template ngFor [ngForOf]="items" [ngForTemplate]="templateRef"></template>`
})
class MyList {
  @Input() data;
  constructor(private templateRef:TemplateRef) {}
}