Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

223
Visualizações
How to use <parent> and <child> component in template recursion in angular

I have 2 components <parent> and <child>. I want to use them in recursive <ng-template>

let say for the sake of simplicity

<parent> component will contain <ul>

<child> component will contain <li>

here is what I have referenced https://gist.github.com/arniebradfo/5cf89c362cc216df6fc1d9ca4d536b72

here is how I have tried

<div>
  <ng-template #recursiveList let-list>
    <span *ngFor="let item of list">
     <!--  commented {{item.title}} -->
      <child [item]="item"/>
      <!-- commented <ul *ngIf="item.children.length > 0"> -->
       <template>
            <parent [item]="item"/>
            <ng-container *ngTemplateOutlet="recursiveList; context:{ $implicit: item.children }"></ng-container>
       </template>
      <!-- commented </ul> -->
    </span>
  </ng-template>
  <ng-container *ngTemplateOutlet="recursiveList; context:{ $implicit: list }"></ng-container>
</div>

but above code does not work properly

my expected result should look like below shown, for a parent with 2 childs

<span>
   <parent>
   <child/> // <--  child 1
   <child/> // <-- child 2
</span>

with html it look like below

<span>
   <ul>Parent</ul>
   <li>child 1</li> // <--  child 1
   <li>child 1</li> // <-- child 2
</span>

it must follow same nesting with any level.

please help me with a demo from https://gist.github.com/arniebradfo/5cf89c362cc216df6fc1d9ca4d536b72

Please help me thanks in advance !!!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

main.component.html

<div>
  <parent *ngFor="let item of list" [item]="item">
</div>

parent.component.html

<span>{{item.name}}</span>
<ul>
  <child *ngFor="let child of item.children" [item]="child">
</ul>

child.component.html

<li>
  <parent [item]="item">
</li>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda