Old topic, but since I struggle trying the same...
Sadly, a component cannot auto delete himself
So we have the following options.
You could generate your component with a componentFactoryResolver
, store them into an array, and then use this array to completely remove those components
<ng-template #chipsContainer></ng-template>
@ViewChild('chipsContainer', { read: ViewContainerRef, static: true })
chipsContainer: ViewContainerRef
loadedChips: any[] = []
addChip(attributes: any) {
let componentFactory = this.componentFactoryResolver.resolveComponentFactory(
AppChipsComponent,
)
let componentRef = this.componentLoaderContainer.createComponent(componentFactory)
let ref: any = componentRef.instance
ref.init(this.loadedChips.lenght) // Will be used to know which index should be removed
this.loadedChips.push(componentRef)
// Subscribing to the EventEmitter from the chip
ref.emitDeletion
.subscribe((index: number) => {
this._removeChip(index)
})
}
private _removeChip(index: number) {
this.loadedChips[index].destroy()
this.loadedChips.splice(index, 1)
}
Then, what you should additionally add to your chip component
export class AppChipsComponent {
private _index: number
@Output() emitDeletion = new EventEmitter<boolean>()
init(index: number) {
this._index = index
}
remove() {
this.emitDeletion.emit(this._index)
}
}
2: Javascript
If the first example seems to complex (And it is, if you want my opinion '^^) you could simply add an id
to the html or anything else that helps you recognize it
then, when clicking on remove()
your can get it, and remove it properly.
<!-- You can use an *ngFor to make it simpler -->
<app-chips id="appChips-0" [id]="appChips-0"></app-chips>
<app-chips id="appChips-1" [id]="appChips-1"></app-chips>
<app-chips id="appChips-2" [id]="appChips-2"></app-chips>
export class AppChipsComponent {
@Input() id!: string
remove() {
document.getElementById(this.id).remove()
}
}
Voilà.