Skip to content

Commit 277dcc4

Browse files
authored
Merge pull request CirclonGroup#787 from alechemy/master
Handle scroll events outside of angular change detection
2 parents a2c2cdf + aff2c09 commit 277dcc4

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

lib/components/tree-viewport.component.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
Component, ElementRef, ViewEncapsulation, HostListener, AfterViewInit, OnInit, OnDestroy
2+
Component, ElementRef, ViewEncapsulation, AfterViewInit, OnInit, OnDestroy, NgZone
33
} from '@angular/core';
44
import { TreeVirtualScroll } from '../models/tree-virtual-scroll.model';
55
import { TREE_EVENTS } from '../constants/events';
@@ -22,10 +22,13 @@ export class TreeViewportComponent implements AfterViewInit, OnInit, OnDestroy {
2222
setViewport = throttle(() => {
2323
this.virtualScroll.setViewport(this.elementRef.nativeElement);
2424
}, 17);
25+
private readonly scrollEventHandler: ($event: Event) => void;
2526

2627
constructor(
2728
private elementRef: ElementRef,
29+
private ngZone: NgZone,
2830
public virtualScroll: TreeVirtualScroll) {
31+
this.scrollEventHandler = this.setViewport.bind(this);
2932
}
3033

3134
ngOnInit() {
@@ -37,18 +40,19 @@ export class TreeViewportComponent implements AfterViewInit, OnInit, OnDestroy {
3740
this.setViewport();
3841
this.virtualScroll.fireEvent({ eventName: TREE_EVENTS.initialized });
3942
});
43+
let el: HTMLElement = this.elementRef.nativeElement;
44+
this.ngZone.runOutsideAngular(() => {
45+
el.addEventListener('scroll', this.scrollEventHandler);
46+
});
4047
}
4148

4249
ngOnDestroy() {
4350
this.virtualScroll.clear();
51+
let el: HTMLElement = this.elementRef.nativeElement;
52+
el.removeEventListener('scroll', this.scrollEventHandler);
4453
}
4554

4655
getTotalHeight() {
4756
return this.virtualScroll.isEnabled() && this.virtualScroll.totalHeight + 'px' || 'auto';
4857
}
49-
50-
@HostListener('scroll', ['$event'])
51-
onScroll(event: Event) {
52-
this.setViewport();
53-
}
5458
}

0 commit comments

Comments
 (0)