Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-content not repeating within ngFor [duplicate]

Tags:

angular

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

like image 664
Eric H Avatar asked Nov 20 '22 01:11

Eric H


1 Answers

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>
like image 85
Tushar Ghosh Avatar answered May 29 '23 12:05

Tushar Ghosh