title: Angular 4 - rxjs BehaviorSubject usage in Service
body: <p>My Service code looks like below - </p>
  
  <p><strong>DataService</strong></p>
  
  <pre><code>@Injectable()
  export class DataService {
  ...
  private serviceRequestDtoSource = new BehaviorSubject&lt;ServiceRequestDto&gt;(null);
  serviceRequestDto$ = this.serviceRequestDtoSource.asObservable();
  ...
  getAccountInfo(serviceRequestDto : ServiceRequestDto){
      let body = JSON.stringify(serviceRequestDto);
      let headers = new Headers({
          'Content-Type': 'application/json'
      });
      let options = new RequestOptions({ headers: headers });
      console.log("In data service.getAccountInfo");
      this.http
          .post(this.clientAccountInfoURL, body, options)
          .map((response : Response) =&gt; { return &lt;AccountDto[]&gt; response.json().accountDtoList})
          .do(data=&gt; console.log('All :'+ JSON.stringify(data)))
          .subscribe( response =&gt; { 
                               this.accountList = response; 
                               this.serviceRequestDto.accountDtoList = this.accountList;
                               this.serviceRequestDtoSource.next(serviceRequestDto);
                           },
                           error =&gt; this.errorMessage = &lt;any&gt;error);
  }
  </code></pre>
  
  <p><strong>Search Component</strong> (which hits above service and also subscribes) looks like below - </p>
  
  <pre><code>onSearch(value) {  
  ...
  this.dataService.getAccountInfo(this.serviceRequestDto); //service call
  this.subscription = this.dataService.serviceRequestDto$
          .subscribe( (serviceRequestDtoValue : ServiceRequestDto) =&gt; {
          // Control doesn't come here..
           this.serviceRequestDto = serviceRequestDtoValue;
           console.log('search.serviceRequestDto.length:'+this.serviceRequestDto.accountDtoList.length);
           console.log('search.serviceRequestDto.accountDtoList.name:'+this.serviceRequestDto.accountDtoList[0].name);
      });
  </code></pre>
  
  <p>As mentioned above, control doesn't come here inside observing component's subscribe method as none of the logs are printed and also I don't get any error in console.Not sure if I'm using BehaviorSubject in proper way. </p>
  
  <p>I tried to follow following links - </p>
  
  <p><a href="https://stackoverflow.com/questions/39494058/angular-2-behavior-subject-vs-observable">Angular 2 - Behavior Subject vs Observable?</a> -> Not entering subscribe as mentioned here.<br>
  <a href="https://blog.kevinchisholm.com/angular2/angular2-http-observables-five-minutes/" rel="nofollow noreferrer">http-observables</a> -> When I tried this way , it does enter subscribe but then throws error for logs - </p>
  
  <blockquote>
    <p><strong>cannot read property xyz of null</strong> 
    as component logs gets printed even before i get data from service.Service logs got printed later after i got above mentioned error.</p>
  </blockquote>
  
  <p>My intention is not to subscribe to the BehaviorSubject from SearchComponent itself which hits the service. I'm just trying to test if i get the values from service in <strong><em>any</em></strong> component.I believe it's as good as subscribing from any other component which needs <em>serviceRequestDto</em> values as syntax for subscribing remains same from all components.</p>
  
  <p><strong>Update 1:</strong></p>
  
  <p>I tried to use ReplaySubject(1) as suggested by Brandon, but still i was getting below error while subscribing to subject in component.</p>
  
  <blockquote>
    <p>TypeError: Cannot read property 'name' of undefined(…)</p>
  </blockquote>
  
  <p>My updated <strong>service</strong> code looks like this now - </p>
  
  <pre><code>serviceRequestDtoSource = new ReplaySubject&lt;ServiceRequestDto&gt;(1);
  getAccountInfo(serviceRequestDto : ServiceRequestDto){
   ...
   //Logic same as earlier
   //I'm getting all values from service here in the logs I print
  
   }
  </code></pre>
  
  <p>Please note earlier I was using following in <strong>service</strong> code - </p>
  
  <pre><code>private serviceRequestDtoSource = new BehaviorSubject&lt;ServiceRequestDto&gt;(null);
  serviceRequestDto$ = this.serviceRequestDtoSource.asObservable();
  </code></pre>
  
  <p>My updated <strong>Search Component</strong> code looks like this - </p>
  
  <pre><code>this.dataService.getAccountInfo(this.serviceRequestDto);
  this.dataService.serviceRequestDtoSource.subscribe((serviceRequestDtoValue : ServiceRequestDto) =&gt; {
  this.serviceRequestDto = serviceRequestDtoValue;
  console.log('search.serviceRequestDto.length:'+this.serviceRequestDto.accountDtoList.length);           
  // prints 'search.serviceRequestDto.length:0'
  console.log('search.serviceRequestDto.accountDtoList   name:'+this.serviceRequestDto.accountDtoList[0].name); // throws 'vendor.bundle.js:5764 ERROR TypeError: Cannot read property 'name' of undefined(…)' error
  });
  </code></pre>
  
  <p>The way earlier component code was subscribing to subject was different and control never went inside component's subscribe though there was no error.</p>
  
  <p>My initial code is different from what I've posted now and it was based on following link - <a href="https://stackoverflow.com/questions/34376854/delegation-eventemitter-or-observable-in-angular2/35568924#35568924">delegation-eventemitter-or-observable-in-angular2</a></p>