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

353
Views
How do you make an Angular 2 component destroy itself?

I am creating multiple dropdown menu components with a button in the parent, but I want those components to have buttons which destroy themselves. I'm sure it's simple, but can't seem to find anything that will do it. I know how to destroy it from the parent, but can't seem to do it from within. Anyone know? And just before it's destroyed, how can I send a message to the parent letting it know? (I have instances of them in the parent, but there are other things in the parent that need a signal)

I'm creating them dynamically using viewContainerRef.createComponent. Template looks like this:

<template item-host></template>

I tried an @Output and got this:

<template item-host [ERROR ->](destroyCheck)="someMethod($event)"></template>
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

use ComponentRef.destroy()

if you creating components dynamically using viewContainerRef.createComponent then you can destroy it with ComponentRef.destroy(). all you need is view reference to itself in the component, like in the example :

parent:

...
const componentRef = this.placeholder.createComponent(
  this.resolver.resolveComponentFactory(SOMECOMP));
componentRef.instance.viewRef = componentRef;
...

child:

viewRef: ComponentRef<SOMECOMP>;
...
this.viewRef.destroy();
...
about 4 years ago · Santiago Trujillo Report

0

Declare a output variable

@Output() destroyCheck:EventEmitter<string>=new EventEmitter<string>();

ngOnDestroy(){

          this.destroyCheck.emit('destroyed');

}

In your parent component handle this way.

<div>
     <child-comp (destroyCheck)="someMethod($event)"> </child-comp>
</div>

Your method should be handled as

someMethod(something){
  console.log(something);
}
about 4 years ago · Santiago Trujillo 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!