title: Angular - how to best handle a Subject in a service and its subscriptions?
body: <p>I am new to <code>rxjs</code> so I am probably not using it as an experienced user would.</p>
  
  <p>I am also using rxjs 5.1.0.</p>
  
  <p>I have a service where I have a <code>BehaviorSubject</code> named <code>file$</code> and an observable from <code>file$</code> called <code>files</code></p>
  
  <pre><code>@Injectable()
  export class UploadService {
    files$: BehaviorSubject&lt;FileToUpload[]&gt; = new BehaviorSubject([]);
    files: Observable&lt;FileToUpload[]&gt; = this.files$.asObservable();
  </code></pre>
  
  <p>This feels a bit weird to have these as member variables.</p>
  
  <p>Is this correct or how better could I do this?</p>
  
  <p>Then in a components <code>onInit</code> I subscribe the <code>files</code> observer:</p>
  
  <pre><code>  ngOnInit() {
      this.fileStateSubscription = this.uploadService.files.subscribe((files) =&gt; {
  </code></pre>
  
  <p>yt
  On <code>ngDestroy</code> I was unsubscribing from subscription:</p>
  
  <pre><code>  ngOnDestroy() {
      this.fileStateSubscription.unsubscribe();
    }
  </code></pre>
  
  <p>Is this correct.  I am concerned about memory leaks etc. and should I be <code>unsubscribing</code> from <code>files$</code> somehow.</p>
  
  <p>I can't unsubscribe from <code>files$</code> in <code>ngOndestroy</code> or at least I would some how need to hook up the subscription again but as it is a member variable this will cause problems.</p>
  
  <p>Should I be subsribing and unsubscribing every time I enter this component?</p>