Reputation: 325
I got "dumb" list component that uses another "dumb" component to render single item. And when I want to pass data to renderers I got to have same property in my list component. For example if I want to have "showTimestamp" boolean property for item I got to do this:
list.template.html
<my-item-renderer *ngFor="let item of items" [showTimestamp]="showTimestamp"></my-item-renderer>
Is there a way to do something to replace part of component template?
I would like to do something like this:
<my-list [items]="items"><ng-content><item-renderer [showTimestamp]="true"></item-renderer></ng-content></my-list>
So my list get just items property and in ng-content gets a renderer with already set showTimestamp variable.
Upvotes: 1
Views: 839
Reputation: 657957
You can use template for this:
<my-list [items]="items">
<template><item-renderer [showTimestamp]="true"></item-renderer></template></my-list>
@Component({
selector: 'my-list',
template: `
<template ngFor [ngForOf]="items" [ngForTemplate]="templateRef"></template>`
})
class MyList {
@Input() data;
constructor(private templateRef:TemplateRef) {}
}
See also
Upvotes: 2