I have created a wrapping component that takes <div *ngFor='let item of items>
as projected content.
I want to replace the immediate bindings of the projected content, with that of the wrapping component.
I am aware that with *ngTemplateOutlet
I can define a associated context object. But I don't know how this can be of use.
Note: The projected content need not be a div
, but it must be an element that supports *ngFor
.
Component({
selector: 'some-component',
template: `
<wrapper>
<div *ngFor="let item of items" <-- make items refer to items in wrapper and not some-component">
<span>{{item}}</span>
</div>
<wrapper>
`
})
class SomeComponent{
items = ['text 1'] <-- replace this ngFor binding
}
Component({
selector: 'wrapper',
template: `
<ng-content></ng-content>
`
})
class Wrapper {
items = ['text 2'] <-- bind ngFor items to this member
}
I expect the span to display 'text 2' not 'text 1'.
@MartinSchneider - Modified example
@Component({
selector: 'my-wrapper',
template: `
<ng-container *ngFor="let item of items">
<ng-content *ngTemplateOutlet="myItemTmpl, context: { $implicit: items}"></ng-content>
</ng-container>
`
})
export class MyWrapperComponent {
@ContentChild('myItem', {static: false}) myItemTmpl: TemplateRef<any>;
items = [
{ id: 0, name: "foo"},
{ id: 1, name: "bar"}
];
}
<my-wrapper>
<div *ngFor="let item of items" #myItem let-items>
<h3>{{item.name}}</h3>
<p>ID: {{item.id}}</p>
</ng-template>
</my-wrapper>