Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!