Skip to content

Commit f6bd9b0

Browse files
anthonnjelbourn
authored andcommitted
fix(expansion): prevent memory leak by calling parent ngOnDestroy (#8410)
1 parent d6f287e commit f6bd9b0

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/lib/expansion/expansion-panel.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ export class MatExpansionPanel extends _MatExpansionPanelMixinBase
142142
}
143143

144144
ngOnDestroy() {
145+
super.ngOnDestroy();
145146
this._inputChanges.complete();
146147
}
147148
}

src/lib/expansion/expansion.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('MatExpansionPanel', () => {
1414
],
1515
declarations: [
1616
PanelWithContent,
17+
PanelWithContentInNgIf,
1718
PanelWithCustomMargin
1819
],
1920
});
@@ -138,6 +139,16 @@ describe('MatExpansionPanel', () => {
138139
expect(arrow.style.transform).toBe('rotate(180deg)', 'Expected 180 degree rotation.');
139140
}));
140141

142+
it('make sure accordion item runs ngOnDestroy when expansion panel is destroyed', () => {
143+
let fixture = TestBed.createComponent(PanelWithContentInNgIf);
144+
fixture.detectChanges();
145+
let destroyedOk = false;
146+
fixture.componentInstance.panel.destroyed.subscribe(() => destroyedOk = true);
147+
fixture.componentInstance.expansionShown = false;
148+
fixture.detectChanges();
149+
expect(destroyedOk).toBe(true);
150+
});
151+
141152
describe('disabled state', () => {
142153
let fixture: ComponentFixture<PanelWithContent>;
143154
let panel: HTMLElement;
@@ -221,6 +232,18 @@ class PanelWithContent {
221232
@ViewChild(MatExpansionPanel) panel: MatExpansionPanel;
222233
}
223234

235+
@Component({
236+
template: `
237+
<div *ngIf="expansionShown">
238+
<mat-expansion-panel>
239+
<mat-expansion-panel-header>Panel Title</mat-expansion-panel-header>
240+
</mat-expansion-panel>
241+
</div>`
242+
})
243+
class PanelWithContentInNgIf {
244+
expansionShown = true;
245+
@ViewChild(MatExpansionPanel) panel: MatExpansionPanel;
246+
}
224247

225248
@Component({
226249
styles: [

0 commit comments

Comments
 (0)