Skip to content

virtual-scroll: switch back to markForCheck since detectChanges #12329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,10 @@ describe('CdkVirtualScrollViewport', () => {

it('should set total content size', fakeAsync(() => {
finishInit(fixture);

viewport.setTotalContentSize(10000);
fixture.detectChanges();
flush();
fixture.detectChanges();

expect(viewport.elementRef.nativeElement.scrollHeight).toBe(10000);
}));
Expand Down
19 changes: 9 additions & 10 deletions src/cdk-experimental/scrolling/virtual-scroll-viewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
ViewEncapsulation,
} from '@angular/core';
import {animationFrameScheduler, fromEvent, Observable, Subject} from 'rxjs';
import {sampleTime, take, takeUntil} from 'rxjs/operators';
import {sampleTime, takeUntil} from 'rxjs/operators';
import {CdkVirtualForOf} from './virtual-for-of';
import {VIRTUAL_SCROLL_STRATEGY, VirtualScrollStrategy} from './virtual-scroll-strategy';

Expand Down Expand Up @@ -301,11 +301,7 @@ export class CdkVirtualScrollViewport implements OnInit, OnDestroy {
if (!this._isChangeDetectionPending) {
this._isChangeDetectionPending = true;
this._ngZone.runOutsideAngular(() => Promise.resolve().then(() => {
if (this._ngZone.isStable) {
this._doChangeDetection();
} else {
this._ngZone.onStable.pipe(take(1)).subscribe(() => this._doChangeDetection());
}
this._doChangeDetection();
}));
}
}
Expand All @@ -314,8 +310,10 @@ export class CdkVirtualScrollViewport implements OnInit, OnDestroy {
private _doChangeDetection() {
this._isChangeDetectionPending = false;

// Apply changes to Angular bindings.
this._ngZone.run(() => this._changeDetectorRef.detectChanges());
// Apply changes to Angular bindings. Note: We must call `markForCheck` to run change detection
// from the root, since the repeated items are content projected in. Calling `detectChanges`
// instead does not properly check the projected content.
this._ngZone.run(() => this._changeDetectorRef.markForCheck());
// Apply the content transform. The transform can't be set via an Angular binding because
// bypassSecurityTrustStyle is banned in Google. However the value is safe, it's composed of
// string literals, a variable that can only be 'X' or 'Y', and user input that is run through
Expand All @@ -330,9 +328,10 @@ export class CdkVirtualScrollViewport implements OnInit, OnDestroy {
}
}

for (const fn of this._runAfterChangeDetection) {
const runAfterChangeDetection = this._runAfterChangeDetection;
this._runAfterChangeDetection = [];
for (const fn of runAfterChangeDetection) {
fn();
}
this._runAfterChangeDetection = [];
}
}
3 changes: 2 additions & 1 deletion src/demo-app/virtual-scroll/virtual-scroll-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Component, ViewEncapsulation} from '@angular/core';
import {ChangeDetectionStrategy, Component, ViewEncapsulation} from '@angular/core';
import {BehaviorSubject} from 'rxjs';


Expand All @@ -22,6 +22,7 @@ type State = {
templateUrl: 'virtual-scroll-demo.html',
styleUrls: ['virtual-scroll-demo.css'],
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class VirtualScrollDemo {
fixedSizeData = Array(10000).fill(50);
Expand Down