title: angular 2 service subscription only updates after second click on component with routerLink(s)
body: <p>Angular 2.0 rc 5, router version 3.0.0 rc1</p>
  
  <p>I have an angular 2 app that uses a service to communicate between components, a parent component that has a <code>&lt;router-outlet&gt;</code> and a child component that has routerLinks. I am using NgClass to update css classes for animations. I added the service so that NgClass updates when I click one of the router links, in addition to changing <code>&lt;router-outlet&gt;</code> output. However I was only able to get the NgClass to update after clicking on the routerLinks twice. Clicking the same routerLink or different ones doesn't matter, as long as it's clicked twice the subscription updates.</p>
  
  <p><a href="http://i.stack.imgur.com/YCmIP.gif" rel="nofollow">page behavior gif</a></p>
  
  <p>My parent component's .ts :</p>
  
  <pre><code>import {Component} from '@angular/core';
  import {NgClass} from '@angular/common';
  import {NavService} from 'components/nav.service';
  import {Subscription} from 'rxjs/Subscription';
  import {ButtonComponent} from 'components/button.component';
  import {MenuComponent} from 'components/menu.component';
  import {CardContainerComponent} from 'components/cardcontainer.component';
  
  @Component({
      selector: 'juan',
      templateUrl: 'app/components/app.component.html',
      directives: [ NgClass ,ButtonComponent, MenuComponent, CardContainerComponent],
      providers: [NavService]
  })
  export class AppComponent {
      isPushed = false;
      subscription: Subscription;
      public initiatePush(){
          this.isPushed = !this.isPushed;
      }
      constructor(private navService: NavService){
          this.subscription = this. navService.navToggle$.subscribe(
              isPushed =&gt; {
                  this.isPushed = isPushed;
              }
          )   
      }
  }
  </code></pre>
  
  <p>the parent component's .html:</p>
  
  <pre><code>&lt;div class="frame" [ngClass]="{'pushed' : isPushed}"&gt;
  
      &lt;div id="o-wrapper" class="o-wrapper"&gt;
          &lt;div class="menu"&gt;
  
          &lt;/div&gt;
          &lt;div class="menu-cover"&gt;
          &lt;/div&gt;
          &lt;animated-button (triggerer)="initiatePush($event)"&gt;&lt;/animated-button&gt;
  
      &lt;/div&gt;
      &lt;router-outlet&gt;&lt;/router-outlet&gt;
      &lt;div class="footer"&gt;&lt;p class="credit"&gt;by &lt;a href="mailto:juan.asher@gmail.com?Subject=Site%20query" class= "email-link"&gt;Juan Asher&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;
      &lt;div id="c-mask" class="c-mask" [ngClass]="{'is-active' : isPushed}"&gt;&lt;/div&gt;
      &lt;main-menu&gt;&lt;/main-menu&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>the service .ts:</p>
  
  <pre><code>import {Injectable}         from '@angular/core';
  import {Subject}        from 'rxjs/Subject';
  
  @Injectable()
  export class NavService{
      private navToggleSource = new Subject&lt;boolean&gt;();
      navToggle$ = this.navToggleSource.asObservable();
      toggleNav(isPushed: boolean) {
          this.navToggleSource.next(isPushed);
      }
  }
  </code></pre>
  
  <p>the child component .ts :</p>
  
  <pre><code>import {Component} from '@angular/core';
  import {SlidingButtonComponent} from './sliding-button.component';
  import {NgClass} from '@angular/common';
  import {NavService} from './nav.service';
  
  @Component({
      selector: 'main-menu',
      templateUrl: 'app/components/menu.component.html',
      directives: [SlidingButtonComponent, NgClass],
      providers: []
  })
  export class MenuComponent {
      isSlided = false;
      isClicked = false;
      public initiateSlide(){
          this.isSlided = !this.isSlided;
      }
      constructor(private navService: NavService) {}
      public toggleFocus(){
          this.isClicked = !this.isClicked;
          this.navService.toggleNav(this.isClicked);  
      }
  }
  </code></pre>
  
  <p>Child component's .html:</p>
  
  <pre><code>&lt;nav class="c-menu"&gt;
      &lt;ul class="c-menu__items"&gt;
          &lt;li class="c-menu__item"&gt;&lt;a (click)="toggleFocus()" class="c-menu__link" routerLink="/card-container" routerLinkActive="active"&gt;Home&lt;/a&gt;&lt;/li&gt;
          &lt;li class="c-menu__item"&gt;&lt;sliding-button (slider)="initiateSlide($event)"&gt;&lt;/sliding-button&gt;&lt;/li&gt;
      &lt;/ul&gt;
  &lt;/nav&gt;
  &lt;div class="login-slider" [ngClass]= "{'is-slided' : isSlided}"&gt;
      &lt;div class="slider-content"&gt;
          &lt;input type="email" class="input-field" placeholder="Email Address"&gt;
  
          &lt;input type="password" class="input-field" placeholder="Password"&gt;  
          &lt;button type="submit" class="button button-block"&gt;Login&lt;/button&gt;
      &lt;/div&gt;
      &lt;div class="slider-tab"&gt;
          &lt;ul class="c-menu__items"&gt;
              &lt;li class="c-menu__item"&gt;&lt;a (click)="toggleFocus()" class="c-menu__link last-item" routerLink="/join" routerLinkActive="active"&gt;Join&lt;/a&gt;&lt;/li&gt;
          &lt;/ul&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Do I need to implement some lifecycle hook to make this work?</p>
  
  <p>Update 20/08/2016</p>
  
  <p>@KrishnrajRana did you mean this?</p>
  
  <pre><code>constructor(private navService: NavService){}
  
  ngOnInit() { 
      this.subscription = this. navService.navToggle$.subscribe(
          isPushed =&gt; {
              this.isPushed = isPushed;
          }
      )   
  }
  </code></pre>
  
  <p>I tried that, it is still having the same behavior.</p>