title: How can I use same instance of an angular service between components in different windows?
body: <p>I have a component <code>mainComponent</code> which calls for a service <code>myService</code> to http request some data update a BehaviorSubject <code>data</code>. Once the request is complete, a new window opens with component <code>otherComponent</code> that will display some info from <code>myService.data</code>. </p>
  
  <p>However, <code>otherComponent</code> does not receive the newest value from <code>data</code> - it receives whatever <code>data</code> is initially set to.</p>
  
  <p><em>mainComponent.ts</em></p>
  
  <pre><code>open() {
      this.myObs = this.myService
      .fetchObservable()
      .subscribe(() =&gt; {
          this.myService.data.subscribe(value =&gt; {
              console.log(value) //logs "new data"                
          });
          this.nativeWindow.open('workflow-editor'); //occurs after data has been updated
      })
  </code></pre>
  
  <p><em>myService.ts</em></p>
  
  <pre><code>public data: BehaviorSubject&lt;string&gt; = new BehaviorSubject&lt;string&gt;("old data");
      fetchObservable(){
          return this.http.get('./url')
          .pipe(
              map(res =&gt; {
                  this.data.next("new data");
                  console.log(this.data.value) //console.logs "new data"
                  return res;
              })
          )
      }
  </code></pre>
  
  <p><em>otherComponent.ts</em></p>
  
  <pre><code>ngOnInit(){
          this.myService.data.subscribe(value =&gt; {
              console.log(value) //console.logs "old data"
          });
  </code></pre>
  
  <p>This problem only occurs when I load <code>otherComponent</code> in a new window. When I load the component in the same window, it correctly logs <code>"new data"</code>. I suspect this is because a new window loads a new version of the app, thus creating a new instance of of <code>myService</code> - one that hasn't had its <code>data</code> value updated. </p>
  
  <p>Is there any way around this? Can I use the same instance of a service in two windows? </p>