We'd like to put a ng-content
inside of an *ngFor
and then specify the contents of the ng-content
when the component containing the ngFor is used. Is it possible?
<li *ngFor="let data of dataSource">
<ng-content></ng-content>
</li>
Demo
Yes, it will not give you proper result. But this can be achieved by ng-template
and TemplateRef
.
I have made a demo. You can see the demo that may help.
child.component.html
<li *ngFor="let rowDetail of dataSource">
<ng-container
*ngIf="headerTemplateRef"
[ngTemplateOutlet]="headerTemplateRef"
[ngTemplateOutletContext]="{$implicit:rowDetail}"
>
</ng-container>
</li>
child.component.ts
@ContentChild('header',{static: false}) headerTemplateRef: TemplateRef<any>;
parent.component.html
<child-app [data]="data">
<ng-template let-rowDetail #header>
<div style="padding-left:35px;">
<div>{{rowDetail.name}}</div>
</div>
</ng-template>
</child-app>
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