Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

224
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda