Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/cdk/overlay/overlay-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class OverlayRef implements PortalOutlet {
private _backdropRef: BackdropRef | null = null;
private _detachContentMutationObserver: MutationObserver | undefined;
private _detachContentAfterRenderRef: AfterRenderRef | undefined;
private _disposed: boolean;

/**
* Reference to the parent of the `_host` at the time it was detached. Used to restore
Expand Down Expand Up @@ -120,6 +121,10 @@ export class OverlayRef implements PortalOutlet {
* @returns The portal attachment result.
*/
attach(portal: Portal<any>): any {
if (this._disposed) {
return null;
}

// Insert the host into the DOM before attaching the portal, otherwise
// the animations module will skip animations on repeat attachments.
this._attachHost();
Expand Down Expand Up @@ -240,6 +245,10 @@ export class OverlayRef implements PortalOutlet {

/** Cleans up the overlay from the DOM. */
dispose(): void {
if (this._disposed) {
return;
}

const isAttached = this.hasAttached();

if (this._positionStrategy) {
Expand All @@ -266,6 +275,7 @@ export class OverlayRef implements PortalOutlet {

this._detachments.complete();
this._completeDetachContent();
this._disposed = true;
}

/** Whether the overlay has attached content. */
Expand Down
7 changes: 7 additions & 0 deletions src/cdk/overlay/overlay.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,13 @@ describe('Overlay', () => {
expect(paneElement.childNodes.length).toBe(0);
}));

it('should do nothing when trying to attach a disposed overlay', () => {
const overlayRef = createOverlayRef(injector);
overlayRef.dispose();
overlayRef.attach(componentPortal);
expect(document.querySelector('.cdk-overlay-pane')).toBeFalsy();
});

describe('positioning', () => {
let config: OverlayConfig;

Expand Down
Loading