|
9 | 9 | import {Direction, Directionality} from '@angular/cdk/bidi';
|
10 | 10 | import {ComponentPortal, Portal, PortalOutlet, TemplatePortal} from '@angular/cdk/portal';
|
11 | 11 | import {
|
| 12 | + AfterRenderRef, |
12 | 13 | ComponentRef,
|
13 | 14 | EmbeddedViewRef,
|
14 | 15 | EnvironmentInjector,
|
@@ -62,6 +63,9 @@ export class OverlayRef implements PortalOutlet {
|
62 | 63 | /** Stream of mouse outside events dispatched to this overlay. */
|
63 | 64 | readonly _outsidePointerEvents = new Subject<MouseEvent>();
|
64 | 65 |
|
| 66 | + /** Reference to the currently-running `afterNextRender` call. */ |
| 67 | + private _afterNextRenderRef: AfterRenderRef | undefined; |
| 68 | + |
65 | 69 | constructor(
|
66 | 70 | private _portalOutlet: PortalOutlet,
|
67 | 71 | private _host: HTMLElement,
|
@@ -134,9 +138,14 @@ export class OverlayRef implements PortalOutlet {
|
134 | 138 | this._scrollStrategy.enable();
|
135 | 139 | }
|
136 | 140 |
|
| 141 | + // We need to clean this up ourselves, because we're passing in an |
| 142 | + // `EnvironmentInjector` below which won't ever be destroyed. |
| 143 | + // Otherwise it causes some callbacks to be retained (see #29696). |
| 144 | + this._afterNextRenderRef?.destroy(); |
| 145 | + |
137 | 146 | // Update the position once the overlay is fully rendered before attempting to position it,
|
138 | 147 | // as the position may depend on the size of the rendered content.
|
139 |
| - afterNextRender( |
| 148 | + this._afterNextRenderRef = afterNextRender( |
140 | 149 | () => {
|
141 | 150 | // The overlay could've been detached before the callback executed.
|
142 | 151 | if (this.hasAttached()) {
|
@@ -250,6 +259,7 @@ export class OverlayRef implements PortalOutlet {
|
250 | 259 | this._outsidePointerEvents.complete();
|
251 | 260 | this._outsideClickDispatcher.remove(this);
|
252 | 261 | this._host?.remove();
|
| 262 | + this._afterNextRenderRef?.destroy(); |
253 | 263 |
|
254 | 264 | this._previousHostParent = this._pane = this._host = null!;
|
255 | 265 |
|
|
0 commit comments