3

Is it possible to insert a component into another parent component and pass data to the child component from the parent component?

  1. User Component
  2. Table Component
  3. Detail Component

in user.html

<app-table>
  <app-detail></app-detail>
</app-table>

in table.html

<div *ngFor="let item of items">
  <ng-content [item]="item"></ng-content> (its my problem)
</div>

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Rafael L R
  • 521
  • 1
  • 4
  • 8

1 Answers1

1

You can use ngTemplateOutlet: https://angular.io/api/common/NgTemplateOutlet

<app-user>
  <app-table [cardTemplate]="pCard"></app-detail>
</app-user>

<ng-template let-record #pCard>
    <div class="card">

    </div>
</ng-template>

Table component:

<div class="nsCard">
    <ng-container *ngTemplateOutlet="cardTemplate; context:{$implicit: record}"></ng-container>
</div>

And inside table component:

@Input() cardTemplate: TemplateRef<any>;

Actually it is more advanced form of ng-template, that provides ability to pass data and many others.

Alex Vovchuk
  • 2,828
  • 4
  • 19
  • 40