title: Angular - Behavior of shared service subscribe
body: <p>Using Angular I have a service to share some variable from different components.
  Like this:</p>
  
  <pre><code>import { Injectable } from "@angular/core";
  import { BehaviorSubject } from "rxjs";
  @Injectable()
  @Injectable({
    providedIn: "root"
  })
  
  /**
   * Service to manage all the global variables in order to use it in different components
   */
  export class SharedService {
  
    // Global variable of the path 
    private rPathSource = new BehaviorSubject(""); // Set up the source
    currentRPath = this.rPathSource.asObservable(); // Make it Observable
  
   constructor() {}
  
    /**
     * Function to change the global path from a component
     * @param path string of the path of the R Folder for the result of the ML
     */
    changeRPath(path: any) {
      this.rPathSource.next(path);
    }
  }
  </code></pre>
  
  <p>Then from a component I subscribe to it. Like this:
  <strong>Component 1</strong></p>
  
  <pre><code>constructor(private shared: SharedService) {}
  
  ngOnInit() {
      this.shared.currentRPath.subscribe(RPath =&gt; {
        this.currentRPath = RPath;
        // HERE I DO A GET REQUEST
      });
    }
  </code></pre>
  
  <p>And from another component I change the variable like this:
  <strong>Component 2</strong></p>
  
  <pre><code>this.shared.changeRPath("");
  </code></pre>
  
  <p>I have a sidenav bar with some buttons and each button change the url and the component loaded with ng content.</p>
  
  <pre><code>&lt;ng-content&gt;&lt;/ng-content&gt;
  </code></pre>
  
  <p>When I press the button to redirect on the component 1 I susbcribe to the variable and the get request is done. Everything is fine.</p>
  
  <p>The problem is when I press the button to redirect on the component 2 the shared variable changes and because I susbcribe on the component 1 it does the get request again. Indeed, the get request is in the callback of the subscribe.</p>
  
  <p>But what is weird is that the component 1 is not load anymore because it is the component 2. It is not supposed to be destroyed when the component changes ?</p>