Skip to content

Commit cfa1d30

Browse files
crisbetojosephperrott
authored andcommitted
fix(badge): badge instances not being cleaned up on destroy (angular#14265)
Fixes the `MatBadge` instances not being collected once the view is destroyed. The issue comes from the fact that when an element is created through the `Renderer2`, Angular adds it to an index (see `DebugRenderer2`), however we never tell it that it should be removed.
1 parent cd8a8b0 commit cfa1d30

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/lib/badge/badge.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,19 @@ export class MatBadge extends _MatBadgeMixinBase implements OnDestroy, CanDisabl
137137
}
138138

139139
ngOnDestroy() {
140-
if (this.description && this._badgeElement) {
141-
this._ariaDescriber.removeDescription(this._badgeElement, this.description);
140+
const badgeElement = this._badgeElement;
141+
142+
if (badgeElement) {
143+
if (this.description) {
144+
this._ariaDescriber.removeDescription(badgeElement, this.description);
145+
}
146+
147+
// When creating a badge through the Renderer, Angular will keep it in an index.
148+
// We have to destroy it ourselves, otherwise it'll be retained in memory.
149+
// @breaking-change 8.0.0 remove _renderer from null.
150+
if (this._renderer && this._renderer.destroyNode) {
151+
this._renderer.destroyNode(badgeElement);
152+
}
142153
}
143154
}
144155

0 commit comments

Comments
 (0)