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

415
Visualizações
Component initialization in ng-template

I am using ng-content to wrap one Angular component in another. The outer component should have full control about when the inner component is rendered or not. However, I ran into a situation, that I don't understand. The outer component uses a template like the following (simplified):

<ng-template>
  <ng-content></ng-content>
</ng-template>

From my understanding, the content should not be rendered at all, as it is contained in the ng-template. In this example stackblitz, you can see that the inner component is not rendered, but it is still initialized. The console log in the inner component's OnInit still shows up in the console.

My question is:

  • Why does this happen?
  • How can I avoid component initialization in this case?
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Angular documentation has information about structure directives: it just renders or not the template, - there is no detail information about loading component inside.

Secondly you pass already compiled and rendered object to the ng-content.

If you want lazy loading for the template, just pass the template:

<app-outer>
  <ng-template>
    <app-inner></app-inner>
  </ng-template>
</app-outer>

And

outer.component.html

<ng-content></ng-content>
<ng-template [ngTemplateOutlet]="template"></ng-template>

outer.component.ts

@Component({
  selector: 'app-outer',
  templateUrl: './outer.component.html'
})
export class OuterComponent {
  @ContentChild(TemplateRef) template: TemplateRef<unknown>;
}
over 4 years ago · Santiago Trujillo 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