diff --git a/src/cdk/scrolling/scroll-dispatcher.spec.ts b/src/cdk/scrolling/scroll-dispatcher.spec.ts index f786c686c12..5ba121a294d 100644 --- a/src/cdk/scrolling/scroll-dispatcher.spec.ts +++ b/src/cdk/scrolling/scroll-dispatcher.spec.ts @@ -112,6 +112,23 @@ describe('ScrollDispatcher', () => { subscription.unsubscribe(); }); + it('should not register the same scrollable twice', () => { + const scrollable = fixture.componentInstance.scrollable; + const scrollSpy = jasmine.createSpy('scroll spy'); + const scrollSubscription = scroll.scrolled(0).subscribe(scrollSpy); + + expect(scroll.scrollContainers.has(scrollable)).toBe(true); + + scroll.register(scrollable); + scroll.deregister(scrollable); + + dispatchFakeEvent(fixture.componentInstance.scrollingElement.nativeElement, 'scroll'); + fixture.detectChanges(); + + expect(scrollSpy).not.toHaveBeenCalled(); + scrollSubscription.unsubscribe(); + }); + }); describe('Nested scrollables', () => { diff --git a/src/cdk/scrolling/scroll-dispatcher.ts b/src/cdk/scrolling/scroll-dispatcher.ts index bf96307d2dd..76cf5545b41 100644 --- a/src/cdk/scrolling/scroll-dispatcher.ts +++ b/src/cdk/scrolling/scroll-dispatcher.ts @@ -52,10 +52,10 @@ export class ScrollDispatcher implements OnDestroy { * @param scrollable Scrollable instance to be registered. */ register(scrollable: CdkScrollable): void { - const scrollSubscription = scrollable.elementScrolled() - .subscribe(() => this._scrolled.next(scrollable)); - - this.scrollContainers.set(scrollable, scrollSubscription); + if (!this.scrollContainers.has(scrollable)) { + this.scrollContainers.set(scrollable, scrollable.elementScrolled() + .subscribe(() => this._scrolled.next(scrollable))); + } } /**