title: Angular 2 - multiple instance of service created
body: <p>I have created AngularJS 2 service and use it in 2 differents components : App-Component &amp; Sub-Component. Each one output property 'log' (a string) of my service.</p>
  <p><strong>StateService class :</strong></p>
  <pre><code>@Injectable ()
  class StateService {
  
      public log : string;
      static count : number = 0;
  
      constructor () {
          this.log = '';
          StateService.count++;
          this.writeToLog ('CREATED '+StateService.count+' at ' + new Date().toString());
      }
  
      public writeToLog (text : string) : void {
          this.log += text + '\n';
      }
  }  
  </code></pre>
  <p><strong>Component :</strong></p>
  <pre><code>@Component ({
      selector : 'Sub-Component',
      template : `&lt;hr&gt;
              This is the Sub-Component !
              &lt;BR&gt;
              StateService Log : 
              &lt;pre&gt;{{ _stateService.log }}&lt;/pre&gt;
              &lt;button (click)=&quot;WriteToLog ()&quot;&gt;Write to log&lt;/button&gt;
              `,
      providers : [StateService]
  })
  
  export class SubComponent {
      constructor (private _stateService : StateService) {
      }
  
      public WriteToLog () : void {
          this._stateService.writeToLog ('From Sub-Component - This is '+new Date().toString());
      }
  }
  </code></pre>
  <p>Live example of code <a href="https://plnkr.co/edit/lREClUJEatbvenMWroTZ?p=preview" rel="noreferrer">here</a></p>
  <p>I except that service is created once and when each component call WriteToLog method, the output is the same in each component but it's not.</p>
  <p><strong>Example of output :</strong></p>
  <p>The App-Component can output this :</p>
  <blockquote>
  <p>Instance 1 - Created at Thu Jan 21 2016 11:43:51</p>
  <p>From App-Component - This is Thu Jan 21 2016 11:43:54</p>
  <p>From App-Component - This is Thu Jan 21 2016 11:43:55</p>
  </blockquote>
  <p>and the Sub-Component can output this :</p>
  <blockquote>
  <p>Instance 2 - Created at Thu Jan 21 2016 11:43:51</p>
  <p>From Sub-Component - This is Thu Jan 21 2016 11:43:57</p>
  <p>From Sub-Component - This is Thu Jan 21 2016 11:43:58</p>
  </blockquote>
  <p>So it appear that 2 instance of service is created (instance 1 + instance 2)</p>
  <p>I only want one instance ;) and when I append string in log, this must appear in both component.</p>
  <p>Thank you for your help</p>