Skip to content

Commit 0d406b3

Browse files
mmalerbajelbourn
authored andcommitted
fix(virtual-scroll): emit on viewChange inside the NgZone (#12873)
1 parent 515912b commit 0d406b3

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/cdk/scrolling/virtual-for-of.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
IterableDiffer,
1818
IterableDiffers,
1919
NgIterable,
20+
NgZone,
2021
OnDestroy,
2122
SkipSelf,
2223
TemplateRef,
@@ -166,14 +167,15 @@ export class CdkVirtualForOf<T> implements CollectionViewer, DoCheck, OnDestroy
166167
/** The set of available differs. */
167168
private _differs: IterableDiffers,
168169
/** The virtual scrolling viewport that these items are being rendered in. */
169-
@SkipSelf() private _viewport: CdkVirtualScrollViewport) {
170+
@SkipSelf() private _viewport: CdkVirtualScrollViewport,
171+
ngZone: NgZone) {
170172
this.dataStream.subscribe(data => {
171173
this._data = data;
172174
this._onRenderedDataChange();
173175
});
174176
this._viewport.renderedRangeStream.pipe(takeUntil(this._destroyed)).subscribe(range => {
175177
this._renderedRange = range;
176-
this.viewChange.next(this._renderedRange);
178+
ngZone.run(() => this.viewChange.next(this._renderedRange));
177179
this._onRenderedDataChange();
178180
});
179181
this._viewport.attach(this);

src/cdk/scrolling/virtual-scroll-viewport.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {dispatchFakeEvent} from '@angular/cdk/testing';
99
import {
1010
Component,
1111
Input,
12+
NgZone,
1213
TrackByFunction,
1314
ViewChild,
1415
ViewContainerRef,
@@ -603,6 +604,13 @@ describe('CdkVirtualScrollViewport', () => {
603604
fixture.destroy();
604605
expect(dispatcher.deregister).toHaveBeenCalledWith(testComponent.viewport);
605606
})));
607+
608+
it('should emit on viewChange inside the Angular zone', fakeAsync(() => {
609+
const zoneTest = jasmine.createSpy('zone test');
610+
testComponent.virtualForOf.viewChange.subscribe(() => zoneTest(NgZone.isInAngularZone()));
611+
finishInit(fixture);
612+
expect(zoneTest).toHaveBeenCalledWith(true);
613+
}));
606614
});
607615

608616
describe('with RTL direction', () => {
@@ -766,6 +774,7 @@ function triggerScroll(viewport: CdkVirtualScrollViewport, offset?: number) {
766774
})
767775
class FixedSizeVirtualScroll {
768776
@ViewChild(CdkVirtualScrollViewport) viewport: CdkVirtualScrollViewport;
777+
@ViewChild(CdkVirtualForOf) virtualForOf: CdkVirtualForOf<any>;
769778
@ViewChild(CdkVirtualForOf, {read: ViewContainerRef}) virtualForViewContainer: ViewContainerRef;
770779

771780
@Input() orientation = 'vertical';

0 commit comments

Comments
 (0)