From c9651bbb058d9f22395ee1bbca14f876b5f6e625 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Sat, 14 Apr 2018 00:32:25 +0200 Subject: [PATCH] fix(button-toggle): focus monitoring not being cleaned up on destroy (#10580) Fixes the focus monitoring for the individual buttons toggles not being cleaned up when they're destroyed, causing the reference to stay inside the `FocusMonitor`. --- src/lib/button-toggle/button-toggle.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/lib/button-toggle/button-toggle.ts b/src/lib/button-toggle/button-toggle.ts index a5c0b1c2ea8..f9f68b0d2d7 100644 --- a/src/lib/button-toggle/button-toggle.ts +++ b/src/lib/button-toggle/button-toggle.ts @@ -19,6 +19,7 @@ import { EventEmitter, forwardRef, Input, + OnDestroy, OnInit, Optional, Output, @@ -332,7 +333,8 @@ export const _MatButtonToggleMixinBase = mixinDisableRipple(MatButtonToggleBase) '[attr.id]': 'id', } }) -export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit, CanDisableRipple { +export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit, + CanDisableRipple, OnDestroy { private _isSingleSelector = false; private _checked = false; @@ -351,7 +353,7 @@ export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit /** Type of the button toggle. Either 'radio' or 'checkbox'. */ _type: ToggleType; - @ViewChild('input') _inputElement: ElementRef; + @ViewChild('input') _inputElement: ElementRef; /** The parent button toggle group (exclusive selection). Optional. */ buttonToggleGroup: MatButtonToggleGroup; @@ -401,7 +403,7 @@ export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit constructor(@Optional() toggleGroup: MatButtonToggleGroup, private _changeDetectorRef: ChangeDetectorRef, - private _elementRef: ElementRef, + private _elementRef: ElementRef, private _focusMonitor: FocusMonitor) { super(); @@ -424,6 +426,10 @@ export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit this._focusMonitor.monitor(this._elementRef.nativeElement, true); } + ngOnDestroy() { + this._focusMonitor.stopMonitoring(this._elementRef.nativeElement); + } + /** Focuses the button. */ focus(): void { this._inputElement.nativeElement.focus();