I have a list component and within the list component, there are children items components that are created dynamically using componentFactoryResolver/createComponent. There is a button that allows you to delete the corresponding item component. How can I achieve this? I don't know how to identify the item component (e.g. what is it's index?) that was clicked and destroying that particular component amongst the list of components.
ListComponent.html
<button (click)="add()></button>
//this is where the item components will be created
ListComponent.ts
add() {
const factory = this.componentFactoryResolver.resolveComponentFactory(ItemComponent);
this.itemRef = this.viewContainerRef.createComponent(factory);
}
ItemComponent.html
<button (click)="deleteItem()"></button>
<list-component></list-component>