I am trying to avoid adding duplicate dynamic component in the angular application. I do not know how to do it. If any one know help to resolve this issue.
home.component.html:
<p>And I'm Lucas Simões</p>
<button type="button" (click)="addComp()">Add component</button>
<div #myDiv></div>
home.component.ts
import { Component, inject, OnInit,ViewChild,ViewContainerRef } from '@angular/core';
import { DataComponent } from '../data/data.component';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css'],
standalone: true,
})
export class HomeComponent implements OnInit{
@ViewChild('myDiv', {static:false, read: ViewContainerRef}) ele!:any;
ngOnInit(){
}
addComp(){
this.ele.createComponent(DataComponent);
}
}
