Skip to content

Commit 3c8edba

Browse files
committed
fix(scroll): memory leak
1 parent 614925d commit 3c8edba

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

src/components/scroll/scroll.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { ChangeDetectionStrategy, Component, ElementRef, Input, ViewEncapsulation } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component, ElementRef, Input, ViewChild, ViewEncapsulation } from '@angular/core';
22

3-
import { isTrueProperty } from '../../util/util';
3+
import { isTrueProperty, assert } from '../../util/util';
44

55
/**
66
* @name Scroll
@@ -22,7 +22,7 @@ import { isTrueProperty } from '../../util/util';
2222
@Component({
2323
selector: 'ion-scroll',
2424
template:
25-
'<div class="scroll-content">' +
25+
'<div class="scroll-content" #scrollContent>' +
2626
'<div class="scroll-zoom-wrapper">' +
2727
'<ng-content></ng-content>' +
2828
'</div>' +
@@ -35,6 +35,7 @@ import { isTrueProperty } from '../../util/util';
3535
encapsulation: ViewEncapsulation.None,
3636
})
3737
export class Scroll {
38+
3839
_scrollX: boolean = false;
3940
_scrollY: boolean = false;
4041
_zoom: boolean = false;
@@ -92,19 +93,11 @@ export class Scroll {
9293
* @hidden
9394
*/
9495
zoomDuration: number = 250;
95-
/**
96-
* @hidden
97-
*/
98-
scrollElement: HTMLElement;
9996

100-
constructor(private _elementRef: ElementRef) {}
97+
/** @internal */
98+
@ViewChild('scrollContent', { read: ElementRef }) _scrollContent: ElementRef;
10199

102-
/**
103-
* @hidden
104-
*/
105-
ngOnInit() {
106-
this.scrollElement = this._elementRef.nativeElement.children[0];
107-
}
100+
constructor(private _elementRef: ElementRef) { }
108101

109102
/**
110103
* @hidden
@@ -114,12 +107,13 @@ export class Scroll {
114107
* undefined if the scroll element doesn't exist.
115108
*/
116109
addScrollEventListener(handler: any) {
117-
if (!this.scrollElement) { return; }
110+
assert(this._scrollContent, 'scroll element is missing');
118111

119-
this.scrollElement.addEventListener('scroll', handler);
112+
const ele = this._scrollContent.nativeElement;
113+
ele.addEventListener('scroll', handler);
120114

121115
return () => {
122-
this.scrollElement.removeEventListener('scroll', handler);
116+
ele.removeEventListener('scroll', handler);
123117
};
124118
}
125119

src/components/scroll/test/basic/main.html

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111

1212
<h2>Horizontal</h2>
1313
<ion-scroll scrollX="true" style="height: 200px">
14-
<div style="height: 200px; width: 2500px; background: url('eight_horns.png') repeat"></div>
14+
<div style="height: 200px; width: 2500px;" class="pattern1"></div>
1515
</ion-scroll>
1616

1717
<h2>Vertical</h2>
1818
<ion-scroll scrollY="true" style="width: 200px; height: 500px">
19-
<div style="height: 2500px; width: 200px; background: url('eight_horns.png') repeat"></div>
19+
<div style="height: 2500px; width: 200px;" class="pattern2"></div>
2020
</ion-scroll>
2121

2222
<h2>Both</h2>
23-
<ion-scroll scrollX="true" scrollY="true" style="width: 100%; height: 500px">
24-
<div style="height: 2500px; width: 2500px; background: url('eight_horns.png') repeat"></div>
23+
<ion-scroll scrollX scrollY style="width: 100%; height: 500px">
24+
<div style="height: 2500px; width: 2500px;" class="pattern3"></div>
2525
</ion-scroll>
2626

2727
</ion-content>
@@ -38,4 +38,29 @@ <h2>Both</h2>
3838
background-color: rgba(0,0,0,0.4);
3939
z-index: 5;
4040
}
41+
42+
.pattern1 {
43+
background:
44+
radial-gradient(circle at 0% 50%, rgba(96, 16, 48, 0) 9px, #613 10px, rgba(96, 16, 48, 0) 11px) 0px 10px,
45+
radial-gradient(at 100% 100%, rgba(96, 16, 48, 0) 9px, #613 10px, rgba(96, 16, 48, 0) 11px),
46+
#8a3;
47+
background-size: 20px 20px;
48+
}
49+
50+
.pattern2 {
51+
background:
52+
linear-gradient(63deg, #999 23%, transparent 23%) 7px 0,
53+
linear-gradient(63deg, transparent 74%, #999 78%),
54+
linear-gradient(63deg, transparent 34%, #999 38%, #999 58%, transparent 62%),
55+
#444;
56+
background-size: 16px 48px;
57+
}
58+
59+
.pattern3 {
60+
background:
61+
linear-gradient(135deg, #708090 22px, #d9ecff 22px, #d9ecff 24px, transparent 24px, transparent 67px, #d9ecff 67px, #d9ecff 69px, transparent 69px),
62+
linear-gradient(225deg, #708090 22px, #d9ecff 22px, #d9ecff 24px, transparent 24px, transparent 67px, #d9ecff 67px, #d9ecff 69px, transparent 69px)0 64px;
63+
background-color:#708090;
64+
background-size: 64px 128px
65+
}
4166
</style>

0 commit comments

Comments
 (0)