66 * found in the LICENSE file at https://angular.io/license
77 */
88
9- import { ElementRef , Injectable , NgZone , Optional , SkipSelf } from '@angular/core' ;
9+ import { ElementRef , Injectable , NgZone , Optional , SkipSelf , OnDestroy } from '@angular/core' ;
1010import { Platform } from '@angular/cdk/platform' ;
1111import { Subject } from 'rxjs/Subject' ;
1212import { Subscription } from 'rxjs/Subscription' ;
@@ -26,7 +26,7 @@ export const DEFAULT_SCROLL_TIME = 20;
2626 * Scrollable references emit a scrolled event.
2727 */
2828@Injectable ( )
29- export class ScrollDispatcher {
29+ export class ScrollDispatcher implements OnDestroy {
3030 constructor ( private _ngZone : NgZone , private _platform : Platform ) { }
3131
3232 /** Subject for notifying that a registered scrollable reference element has been scrolled. */
@@ -97,14 +97,18 @@ export class ScrollDispatcher {
9797 subscription . unsubscribe ( ) ;
9898 this . _scrolledCount -- ;
9999
100- if ( this . _globalSubscription && ! this . _scrolledCount ) {
101- this . _globalSubscription . unsubscribe ( ) ;
102- this . _globalSubscription = null ;
100+ if ( ! this . _scrolledCount ) {
101+ this . _removeGlobalListener ( ) ;
103102 }
104103 } ;
105104 } ) : observableOf < void > ( ) ;
106105 }
107106
107+ ngOnDestroy ( ) {
108+ this . _removeGlobalListener ( ) ;
109+ this . scrollContainers . forEach ( ( _ , container ) => this . deregister ( container ) ) ;
110+ }
111+
108112 /**
109113 * Returns an observable that emits whenever any of the
110114 * scrollable ancestors of an element are scrolled.
@@ -146,12 +150,20 @@ export class ScrollDispatcher {
146150 return false ;
147151 }
148152
149- /** Sets up the global scroll and resize listeners. */
153+ /** Sets up the global scroll listeners. */
150154 private _addGlobalListener ( ) {
151155 this . _globalSubscription = this . _ngZone . runOutsideAngular ( ( ) => {
152156 return fromEvent ( window . document , 'scroll' ) . subscribe ( ( ) => this . _scrolled . next ( ) ) ;
153157 } ) ;
154158 }
159+
160+ /** Cleans up the global scroll listener. */
161+ private _removeGlobalListener ( ) {
162+ if ( this . _globalSubscription ) {
163+ this . _globalSubscription . unsubscribe ( ) ;
164+ this . _globalSubscription = null ;
165+ }
166+ }
155167}
156168
157169/** @docs -private */
0 commit comments