title: Understanding Angular Shared service
body: <p>I want to keep total amount of my component updated as it's changing, and I'm wondering is this right:</p>
  
  <p>I have a <code>shared.service.ts</code> which is injected into my component so it looks like this (my shared service):</p>
  
  <pre><code>shared.service.ts
  
  private sumEmit= new BehaviorSubject&lt;number&gt;(0);
  
  public updateValues() {
  this.sum= 0;
  this.sum+= +(price*quant).toFixed(2);
  this.sumEmit.next(this.sum);
  }
  </code></pre>
  
  <p>Method to get total amount also in service:</p>
  
  <pre><code> getTotal(): BehaviorSubject&lt;number&gt; {
      return this.sumEmit;
    }
  </code></pre>
  
  <p>So, every time <code>updateValues()</code> method is called, it will emit value of sum, that's right?</p>
  
  <p>And in my component which need to retreive value everytime value is changed I did this:</p>
  
  <pre><code>mycomponent.component.ts
  </code></pre>
  
  <p>First of all I injected shared service, after that in <code>ngOnInit</code> life cycle hool I wrote:</p>
  
  <pre><code>// simple variable to hold value from service
  
  total : number;
  
  
  ngOnInit() 
  {
      this.total = this.SharedService.getTotal();
  }
  </code></pre>
  
  <p>Does this mean that my value will be up to date everytime total amount is changed? </p>
  
  <p>Most confusing part for me is that I called this method only once in a <code>ngOnInit</code> which is rendered when component is opened, but value is kept updated.. Could anyone explain me this please? How come variable in my component is aware when value is changed in service? Which part is responsible for that?</p>
  
  <p>Thanks</p>