diff --git a/src/lib/core/overlay/scroll/scroll-dispatcher.spec.ts b/src/lib/core/overlay/scroll/scroll-dispatcher.spec.ts index 8f8282e58117..6d80ef09f201 100644 --- a/src/lib/core/overlay/scroll/scroll-dispatcher.spec.ts +++ b/src/lib/core/overlay/scroll/scroll-dispatcher.spec.ts @@ -89,6 +89,19 @@ describe('Scroll Dispatcher', () => { expect(spy).not.toHaveBeenCalled(); subscription.unsubscribe(); }); + + it('should be able to unsubscribe from the global scrollable', () => { + const spy = jasmine.createSpy('global scroll callback'); + const subscription = scroll.scrolled(0, spy); + + dispatchFakeEvent(document, 'scroll'); + expect(spy).toHaveBeenCalledTimes(1); + + subscription.unsubscribe(); + dispatchFakeEvent(document, 'scroll'); + + expect(spy).toHaveBeenCalledTimes(1); + }); }); describe('Nested scrollables', () => { diff --git a/src/lib/core/overlay/scroll/scroll-dispatcher.ts b/src/lib/core/overlay/scroll/scroll-dispatcher.ts index abd8ea55410e..2315d7cc9332 100644 --- a/src/lib/core/overlay/scroll/scroll-dispatcher.ts +++ b/src/lib/core/overlay/scroll/scroll-dispatcher.ts @@ -81,7 +81,9 @@ export class ScrollDispatcher { // Note that we need to do the subscribing from here, in order to be able to remove // the global event listeners once there are no more subscriptions. - return observable.subscribe(callback).add(() => { + let subscription = observable.subscribe(callback); + + subscription.add(() => { this._scrolledCount--; if (this._globalSubscription && !this.scrollableReferences.size && !this._scrolledCount) { @@ -89,6 +91,8 @@ export class ScrollDispatcher { this._globalSubscription = null; } }); + + return subscription; } /** Returns all registered Scrollables that contain the provided element. */