title: Angular Service - subscribe to component OnDestroy
body: <p>I have a service (<code>MyService</code>) that is injected in my <code>MyComponent</code>.  I want <code>MyComponent</code> to call a function on <code>MyService</code>, and pass some sort of a reference to this particular instance of <code>MyComponent</code> so that the service knows when the instance has been <code>destroyed</code>.</p>
  
  <p>What is the proper object to pass to <code>MyService</code> from <code>MyComponent</code> so that <code>MyService</code> can know when the component is destroyed?  I am assuming there is a some sort of an observable <code>MyService</code> can subscribe to in order to get this information.</p>
  
  <p><strong>EDIT:  Some more context</strong></p>
  
  <p><code>MyService</code>is in charge of creating <code>@angular/cdk Portal</code> and has two functions:</p>
  
  <p><code>attachComponent&lt;T&gt;(component: ComponentType&lt;T&gt;, data?: ComponentData&lt;any&gt;)</code> </p>
  
  <p>and </p>
  
  <p><code>attachTemplate&lt;T&gt;(templateRef: TemplateRef&lt;T&gt;, viewContainerRef: ViewContainerRef)</code>.</p>
  
  <p>This service emits an observable that <code>MyHeaderComponent</code> subscribes to, in order to reflect some content that <code>MyComponent</code> has asked for it to render.  However, for clean-up reasons and so that every single other component doesn't have to import this service and clear the clontents of the header, I want to know when the caller of that function has been destroyed, and do it in the service so I only have to code it there.  Basically, the idea is, when the caller to either <code>attachTemplate</code> or <code>attachComponent</code> is destroyed, then the contents should be automatically cleared.  I'm trying to centralize this logic in <code>MyService</code> so that not every single component that injects <code>MyService</code> and uses this functionality has to implement some sort of a <code>clearContent()</code> function in ngOnDestroy.</p>