title: Tracking scroll position and notifying other components about it
body: <p>Is there an easy way to track the browser scroll position and notify more than a single component about it?</p>
  
  <p>Use case: On scroll I want to be able to change classes of various elements on the page based upon where I am. In a previous version of angular it was somewhat possible through a plugin (same for jQuery). Of course, there is the option of writing bare JS to initialize it on application start and emit an event, but that sounds dirty, and event emission is pretty expensive for this type of thing.</p>
  
  <p>What are my options here?</p>
  
  <hr>
  
  <p>UPDATE (after suggestions):</p>
  
  <p>Here is what I tried:</p>
  
  <p>I created a component:</p>
  
  <pre><code>import {Component} from "angular2/core";
  
  @Component({
      selector: '[track-scroll]',
      host: {'(window:scroll)': 'track($event)'},
      template: ''
  })
  
  export class TrackScrollComponent {
      track($event) {
          console.debug("Scroll Event", $event);
      }
  }
  </code></pre>
  
  <p>added an attribute to the main directive of an app:</p>
  
  <pre><code>&lt;priz-app track-scroll&gt;
  </code></pre>
  
  <p>and added the component as one of the providers in the top component:</p>
  
  <pre><code>import {TrackScrollComponent} from "../../shared/components/track-scroll.component";
  
  @Component({
    selector: 'priz-app',
    moduleId: module.id,
    templateUrl: './app.component.html',
    directives: [ROUTER_DIRECTIVES, SecureRouterOutlet, AppHeader, TrackScrollComponent],
    providers: [AuthenticationService]
  })
  </code></pre>
  
  <p>Still nothing...</p>
  
  <hr>
  
  <p>ANOTHER UPDATE:</p>
  
  <p>Moved <code>track-scroll</code> to one of the div elements of the main template:</p>
  
  <pre><code>&lt;div class="container-fluid" track-scroll&gt;
      &lt;div class="row"&gt;
          &lt;div class="col-md-12"&gt;
              &lt;app-header&gt;&lt;/app-header&gt;
              &lt;secure-outlet signin="Login" unauthorized="AccessDenied"&gt;&lt;/secure-outlet&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>And now the app loads with a completely empty screen.
  FUN FUN FUN...</p>
  
  <hr>
  
  <p>FINAL SOLUTION (that worked for me).</p>
  
  <ol>
  <li>Define a directive:</li>
  </ol>
  
  
  
  <pre><code>import {Directive} from "angular2/core";
  
  @Directive({
      selector: '[track-scroll]',
      host: {'(window:scroll)': 'track($event)'}
  })
  
  export class TrackScrollDirective {
      track($event: Event) {
          console.debug("Scroll Event", $event);
      }
  }
  </code></pre>
  
  <ol start="2">
  <li>Add it as a directive everywhere that uses it:</li>
  </ol>
  
  
  
  <pre><code>directives: [TrackScrollDirective]
  </code></pre>
  
  <ol start="3">
  <li>Add the attribute to each element we want to track the event:</li>
  </ol>
  
  
  
  <pre><code>&lt;div class="col-md-12" track-scroll&gt;
  </code></pre>