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

225
Visualizações
Angular: render numerous components on (click)

I do not want to render a new component or go to a new route, this is not my intention. I cannot use a single variable with an *ngIf to handle rendering the component as I cannot predict the amount of variables I will need to render.

Here is the situation.

<div ngFor="let stuff of lotsOfStuff">
    <div (click)="generateAnotherComponent()">
         <span>some basic info</span>
         <my-child-component></my-child-component> //component to render on click
    </div>
    <div (click)="generateAnotherComponent()">
          <span>some basic info</span>
          <my-child-component></my-child-component> //component to render on click
    </div>
</div>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can generate the component dynamically.

There are various ways to achieve this, but one would be to use a template variable placed on an element, for example

<div #ChildInsertionPoint></div>

And then targeting this "insertion point" as where you'll place the newly generated components by using ViewContainerRef:

@ViewChild('ChildInsertionPoint', { read: ViewContainerRef }) 
    childInsertionPoint: ViewContainerRef;

generateAnotherComponent() {
    this.childInsertionPoint.createComponent(ChildComponent);
}

Dynamic component generation became a lot easier with Angular 13 (that's what I've assumed you're using). But if not Finally, I'm assuming that you will bind your click event to a button instead of the div element itself.

Here's a small example on Stackblitz

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