How can I transclude/project into a slot that is within a loop, and have the projected content able to access the loop variables?
Say I have a base component with the following
<tr *ngFor="let data of items">
<td>{{data.title}}</td>
<ng-content select="[slot]"></ng-content>
</tr>
And a child component that uses the transclusion slot "slot"
<parent [items]="items">
<ng-container slot>
<td>{{data.category}}</td>
<td>{{data.number}}</td>
</ng-container>
</parent>
The HTML I'd like generated is
<tr>
<td>{{data.title}}</td>
<td>{{data.category}}</td>
<td>{{data.number}}</td>
</tr>
But what actually happens is that "data" is not defined in the child component, which makes sense. Is there any way I can get it to work like this?
Using TemplateRef
it is possible to declare template variables acting between two components declaratively, on templates level. The following solution does not fully match your "slot" infrastructure but could be useful to further investigation.
list.component.ts
import { Component, Input, ContentChild, TemplateRef } from '@angular/core';
@Component({
selector: 'list',
template: `
<tr class="box" *ngFor="let data of items">
<td>{{data.title}}</td>
<ng-template
[ngTemplateOutlet]="template"
[ngTemplateOutletContext]="{ $implicit: data }">
</ng-template>
</tr>`
})
export class ListComponent {
@Input() items;
@ContentChild(TemplateRef) template: TemplateRef;
constructor() { }
}
wrapper.component.ts
import { Component, ContentChild, TemplateRef } from '@angular/core';
@Component({
selector: 'wrapper',
template: `
<table>
<list [items]="items">
<ng-template let-data>
<td>{{data.category}}</td>
<td>{{data.number}}</td>
</ng-template>
</list>
</table>`
})
export class WrapperComponent {
items = [
{ title: 'T1', category: 'C1', number: 'N1' },
{ title: 'T2', category: 'C2', number: 'N2' },
{ title: 'T3', category: 'C3', number: 'N3' }
];
@ContentChild(TemplateRef) template: TemplateRef;
constructor() { }
}
I also created a Plunker demo...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With