Skip to content

Commit 32db2ba

Browse files
michastreppelmmalerba
authored andcommitted
fix(tooltip): remove native event listener on component destroy (#5144)
* fix(tooltip): remove native event listener on component destroy Component should clean up events it has registered to. Closes #4499 * Break at higher syntactic level * Undo accidental formatting
1 parent 68241a8 commit 32db2ba

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/lib/tooltip/tooltip.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ export class MdTooltip implements OnDestroy {
170170
get _matClass() { return this.tooltipClass; }
171171
set _matClass(v) { this.tooltipClass = v; }
172172

173+
private _enterListener: Function;
174+
private _leaveListener: Function;
175+
173176
constructor(
174177
private _overlay: Overlay,
175178
private _elementRef: ElementRef,
@@ -183,8 +186,10 @@ export class MdTooltip implements OnDestroy {
183186
// The mouse events shouldn't be bound on iOS devices, because
184187
// they can prevent the first tap from firing its click event.
185188
if (!_platform.IOS) {
186-
_renderer.listen(_elementRef.nativeElement, 'mouseenter', () => this.show());
187-
_renderer.listen(_elementRef.nativeElement, 'mouseleave', () => this.hide());
189+
this._enterListener =
190+
_renderer.listen(_elementRef.nativeElement, 'mouseenter', () => this.show());
191+
this._leaveListener =
192+
_renderer.listen(_elementRef.nativeElement, 'mouseleave', () => this.hide());
188193
}
189194
}
190195

@@ -195,6 +200,11 @@ export class MdTooltip implements OnDestroy {
195200
if (this._tooltipInstance) {
196201
this._disposeTooltip();
197202
}
203+
// Clean up the event listeners set in the constructor
204+
if (!this._platform.IOS) {
205+
this._enterListener();
206+
this._leaveListener();
207+
}
198208
}
199209

200210
/** Shows the tooltip after the delay in ms, defaults to tooltip-delay-show or 0ms if no input */

0 commit comments

Comments
 (0)