Skip to content

Commit 9b18b5f

Browse files
authored
fix(mem-leak): Fix potential memory leaks. (#15309)
1 parent ca888ee commit 9b18b5f

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

projects/igniteui-angular/src/lib/directives/button/button.directive.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
Output,
88
Renderer2,
99
booleanAttribute,
10-
AfterContentInit
10+
AfterContentInit,
11+
OnDestroy
1112
} from '@angular/core';
1213
import { mkenum } from '../../core/utils';
1314
import { IBaseEventArgs } from '../../core/utils';
@@ -46,7 +47,7 @@ export type IgxButtonType = typeof IgxButtonType[keyof typeof IgxButtonType];
4647
selector: '[igxButton]',
4748
standalone: true
4849
})
49-
export class IgxButtonDirective extends IgxButtonBaseDirective implements AfterContentInit {
50+
export class IgxButtonDirective extends IgxButtonBaseDirective implements AfterContentInit, OnDestroy {
5051
private static ngAcceptInputType_type: IgxButtonType | '';
5152

5253
/**
@@ -92,6 +93,12 @@ export class IgxButtonDirective extends IgxButtonBaseDirective implements AfterC
9293
*/
9394
private _selected = false;
9495

96+
private emitSelected() {
97+
this.buttonSelected.emit({
98+
button: this
99+
});
100+
}
101+
95102
/**
96103
* Gets or sets whether the button is selected.
97104
* Mainly used in the IgxButtonGroup component and it will have no effect if set separately.
@@ -121,11 +128,11 @@ export class IgxButtonDirective extends IgxButtonBaseDirective implements AfterC
121128
}
122129

123130
public ngAfterContentInit() {
124-
this.nativeElement.addEventListener('click', () => {
125-
this.buttonSelected.emit({
126-
button: this
127-
});
128-
});
131+
this.nativeElement.addEventListener('click', this.emitSelected.bind(this));
132+
}
133+
134+
public ngOnDestroy(): void {
135+
this.nativeElement.removeEventListener('click', this.emitSelected);
129136
}
130137

131138
/**

projects/igniteui-angular/src/lib/input-group/input-group.component.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,13 @@ export class IgxInputGroupComponent implements IgxInputGroupBase, AfterViewInit
220220
private themeToken: ThemeToken
221221
) {
222222
this._theme = this.themeToken.theme;
223-
224-
const { unsubscribe } = this.themeToken.onChange((theme) => {
223+
const themeChange = this.themeToken.onChange((theme) => {
225224
if (this._theme !== theme) {
226225
this._theme = theme;
227226
this.cdr.detectChanges();
228227
}
229228
});
230-
231-
this._destroyRef.onDestroy(() => unsubscribe);
229+
this._destroyRef.onDestroy(() => themeChange.unsubscribe());
232230
}
233231

234232
/** @hidden */

0 commit comments

Comments
 (0)