You could try the following solution:
@Component({
selector: 'DropDownList',
template: `
<li *ngFor="let item of items" (click)="handleOnSelect(item)">
<template [ngTemplateOutlet]="itemWrapper" [ngOutletContext]="{ $implicit: item }">
</template>
</li>`
})
export class DropDownListComponent {
@Input() itemWrapper: TemplateRef<any>;
@Input() items: any;
handleOnSelect(item) {
console.log('clicked');
}
}
@Component({
selector: 'DropDown',
template: `
<div>
<ul>
<DropDownList [items]="items" [itemWrapper]="itemWrapper">
</DropDownList>
</ul>
</div>`
})
export class DropDownComponent {
@Input() items: string[];
@ContentChild(TemplateRef) itemWrapper: TemplateRef<any>;
}
@Component({
selector: 'my-app',
template: `
<DropDown [items]="items">
<template let-item>
<h1>item: {{item}}</h1>
</template>
</DropDown>
`
})
export class App {
items = ['this','is','a','test'];
}
Plunker Example
The ngTemplateOutlet(^2.0.0-rc.2) directive has the same functionality as your custom directive NgWrapper
See also related questions:
- Creating a dynamic repeater with ng-content transclusion with Angular2
- Switch html templates dynamically during runtime on user action in angular 2