1
1
import {
2
- Component , ElementRef , ViewEncapsulation , HostListener , AfterViewInit , OnInit , OnDestroy
2
+ Component , ElementRef , ViewEncapsulation , AfterViewInit , OnInit , OnDestroy , NgZone
3
3
} from '@angular/core' ;
4
4
import { TreeVirtualScroll } from '../models/tree-virtual-scroll.model' ;
5
5
import { TREE_EVENTS } from '../constants/events' ;
@@ -22,10 +22,13 @@ export class TreeViewportComponent implements AfterViewInit, OnInit, OnDestroy {
22
22
setViewport = throttle ( ( ) => {
23
23
this . virtualScroll . setViewport ( this . elementRef . nativeElement ) ;
24
24
} , 17 ) ;
25
+ private readonly scrollEventHandler : ( $event : Event ) => void ;
25
26
26
27
constructor (
27
28
private elementRef : ElementRef ,
29
+ private ngZone : NgZone ,
28
30
public virtualScroll : TreeVirtualScroll ) {
31
+ this . scrollEventHandler = this . setViewport . bind ( this ) ;
29
32
}
30
33
31
34
ngOnInit ( ) {
@@ -37,18 +40,19 @@ export class TreeViewportComponent implements AfterViewInit, OnInit, OnDestroy {
37
40
this . setViewport ( ) ;
38
41
this . virtualScroll . fireEvent ( { eventName : TREE_EVENTS . initialized } ) ;
39
42
} ) ;
43
+ let el : HTMLElement = this . elementRef . nativeElement ;
44
+ this . ngZone . runOutsideAngular ( ( ) => {
45
+ el . addEventListener ( 'scroll' , this . scrollEventHandler ) ;
46
+ } ) ;
40
47
}
41
48
42
49
ngOnDestroy ( ) {
43
50
this . virtualScroll . clear ( ) ;
51
+ let el : HTMLElement = this . elementRef . nativeElement ;
52
+ el . removeEventListener ( 'scroll' , this . scrollEventHandler ) ;
44
53
}
45
54
46
55
getTotalHeight ( ) {
47
56
return this . virtualScroll . isEnabled ( ) && this . virtualScroll . totalHeight + 'px' || 'auto' ;
48
57
}
49
-
50
- @HostListener ( 'scroll' , [ '$event' ] )
51
- onScroll ( event : Event ) {
52
- this . setViewport ( ) ;
53
- }
54
58
}
0 commit comments