@@ -131,9 +131,6 @@ export function MAT_TOOLTIP_DEFAULT_OPTIONS_FACTORY(): MatTooltipDefaultOptions
131
131
@Directive ( {
132
132
selector : '[matTooltip]' ,
133
133
exportAs : 'matTooltip' ,
134
- host : {
135
- '(keydown)' : '_handleKeydown($event)' ,
136
- } ,
137
134
} )
138
135
export class MatTooltip implements OnDestroy , OnInit {
139
136
_overlayRef : OverlayRef | null ;
@@ -285,6 +282,10 @@ export class MatTooltip implements OnDestroy, OnInit {
285
282
_ngZone . run ( ( ) => this . show ( ) ) ;
286
283
}
287
284
} ) ;
285
+
286
+ _ngZone . runOutsideAngular ( ( ) => {
287
+ _elementRef . nativeElement . addEventListener ( 'keydown' , this . _handleKeydown ) ;
288
+ } ) ;
288
289
}
289
290
290
291
/**
@@ -299,6 +300,8 @@ export class MatTooltip implements OnDestroy, OnInit {
299
300
* Dispose the tooltip when destroyed.
300
301
*/
301
302
ngOnDestroy ( ) {
303
+ const nativeElement = this . _elementRef . nativeElement ;
304
+
302
305
clearTimeout ( this . _touchstartTimeout ) ;
303
306
304
307
if ( this . _overlayRef ) {
@@ -307,16 +310,17 @@ export class MatTooltip implements OnDestroy, OnInit {
307
310
}
308
311
309
312
// Clean up the event listeners set in the constructor
313
+ nativeElement . removeEventListener ( 'keydown' , this . _handleKeydown ) ;
310
314
this . _passiveListeners . forEach ( ( listener , event ) => {
311
- this . _elementRef . nativeElement . removeEventListener ( event , listener , passiveListenerOptions ) ;
315
+ nativeElement . removeEventListener ( event , listener , passiveListenerOptions ) ;
312
316
} ) ;
313
317
this . _passiveListeners . clear ( ) ;
314
318
315
319
this . _destroyed . next ( ) ;
316
320
this . _destroyed . complete ( ) ;
317
321
318
- this . _ariaDescriber . removeDescription ( this . _elementRef . nativeElement , this . message ) ;
319
- this . _focusMonitor . stopMonitoring ( this . _elementRef ) ;
322
+ this . _ariaDescriber . removeDescription ( nativeElement , this . message ) ;
323
+ this . _focusMonitor . stopMonitoring ( nativeElement ) ;
320
324
}
321
325
322
326
/** Shows the tooltip after the delay in ms, defaults to tooltip-delay-show or 0ms if no input */
@@ -356,12 +360,15 @@ export class MatTooltip implements OnDestroy, OnInit {
356
360
return ! ! this . _tooltipInstance && this . _tooltipInstance . isVisible ( ) ;
357
361
}
358
362
359
- /** Handles the keydown events on the host element. */
360
- _handleKeydown ( e : KeyboardEvent ) {
361
- if ( this . _isTooltipVisible ( ) && e . keyCode === ESCAPE && ! hasModifierKey ( e ) ) {
362
- e . preventDefault ( ) ;
363
- e . stopPropagation ( ) ;
364
- this . hide ( 0 ) ;
363
+ /**
364
+ * Handles the keydown events on the host element.
365
+ * Needs to be an arrow function so that we can use it in addEventListener.
366
+ */
367
+ private _handleKeydown = ( event : KeyboardEvent ) => {
368
+ if ( this . _isTooltipVisible ( ) && event . keyCode === ESCAPE && ! hasModifierKey ( event ) ) {
369
+ event . preventDefault ( ) ;
370
+ event . stopPropagation ( ) ;
371
+ this . _ngZone . run ( ( ) => this . hide ( 0 ) ) ;
365
372
}
366
373
}
367
374
0 commit comments