How can you destroy yourself with Angular 2?

I am creating multiple dropdown menu components with a button in the parent, but I want these components to have buttons that destroy themselves. I'm sure this is simple, but can't seem to find anything that will do it. I know how to destroy it from the parent, but cannot do it from the inside. Somebody knows? And just before destroying him, how can I send a message to the parents letting him know? (I have instances of these in the parent, but there are other things in the parent that need a signal)

I create them dynamically using viewContainerRef.createComponent

. The template looks like this:

<template item-host></template>

      

I tried @Output and got this:

<template item-host [ERROR ->](destroyCheck)="someMethod($event)"></template>

      

+3


source to share


1 answer


Declare output variable

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

ngOnDestroy(){

          this.destroyCheck.emit('destroyed');

}

      

Your parent component handles this path.



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

      

Your method should be handled like

someMethod(something){
  console.log(something);
}

      

+4


source







All Articles