6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { Directive , Output , Input , AfterContentInit , EventEmitter } from '@angular/core' ;
9
+ import { Directive , Output , Input , AfterContentInit , EventEmitter , OnDestroy } from '@angular/core' ;
10
10
import { coerceBooleanProperty , BooleanInput } from '@angular/cdk/coercion' ;
11
11
import { CdkMenuPanel } from './menu-panel' ;
12
12
import { CdkMenuGroup } from './menu-group' ;
13
13
import { MenuItem } from './menu-item-interface' ;
14
+ import { takeUntil } from 'rxjs/operators' ;
14
15
15
16
/**
16
17
* Directive which provides behavior for an element which when clicked:
@@ -36,7 +37,7 @@ import {MenuItem} from './menu-item-interface';
36
37
'[attr.aria-disabled]' : 'disabled || null' ,
37
38
} ,
38
39
} )
39
- export class CdkMenuItem implements AfterContentInit , MenuItem {
40
+ export class CdkMenuItem implements AfterContentInit , MenuItem , OnDestroy {
40
41
/** Template reference variable to the menu this trigger opens */
41
42
@Input ( 'cdkMenuTriggerFor' ) _menuPanel ?: CdkMenuPanel ;
42
43
@@ -66,6 +67,9 @@ export class CdkMenuItem implements AfterContentInit, MenuItem {
66
67
/** Emits when the attached submenu is opened */
67
68
@Output ( ) opened : EventEmitter < void > = new EventEmitter ( ) ;
68
69
70
+ /** Emits when the component gets destroyed */
71
+ private readonly _destroyed : EventEmitter < void > = new EventEmitter ( ) ;
72
+
69
73
constructor (
70
74
/** reference a parent CdkMenuGroup component */
71
75
private readonly _menuGroup : CdkMenuGroup
@@ -74,7 +78,9 @@ export class CdkMenuItem implements AfterContentInit, MenuItem {
74
78
/** Configure event subscriptions */
75
79
ngAfterContentInit ( ) {
76
80
if ( this . role !== 'menuitem' ) {
77
- this . _menuGroup . change . subscribe ( ( button : CdkMenuItem ) => this . _toggleCheckedState ( button ) ) ;
81
+ this . _menuGroup . change
82
+ . pipe ( takeUntil ( this . _destroyed ) )
83
+ . subscribe ( ( button : MenuItem ) => this . _toggleCheckedState ( button ) ) ;
78
84
}
79
85
}
80
86
@@ -109,14 +115,19 @@ export class CdkMenuItem implements AfterContentInit, MenuItem {
109
115
/**
110
116
* Toggle the checked state of the menuitemradio or menuitemcheckbox component
111
117
*/
112
- private _toggleCheckedState ( selected : CdkMenuItem ) {
118
+ private _toggleCheckedState ( selected : MenuItem ) {
113
119
if ( this . role === 'menuitemradio' ) {
114
120
this . checked = selected === this ;
115
121
} else if ( this . role === 'menuitemcheckbox' && selected === this ) {
116
122
this . checked = ! this . checked ;
117
123
}
118
124
}
119
125
126
+ ngOnDestroy ( ) {
127
+ this . _destroyed . next ( ) ;
128
+ this . _destroyed . complete ( ) ;
129
+ }
130
+
120
131
static ngAcceptInputType_checked : BooleanInput ;
121
132
static ngAcceptInputType_disabled : BooleanInput ;
122
133
}
0 commit comments