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
5 changes: 5 additions & 0 deletions src/lib/menu/menu-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from '@angular/core';
import {TemplatePortal, DomPortalOutlet} from '@angular/cdk/portal';
import {DOCUMENT} from '@angular/common';
import {Subject} from 'rxjs';

/**
* Menu content that will be rendered lazily once the menu is opened.
Expand All @@ -29,6 +30,9 @@ export class MatMenuContent implements OnDestroy {
private _portal: TemplatePortal<any>;
private _outlet: DomPortalOutlet;

/** Emits when the menu content has been attached. */
_attached = new Subject<void>();

constructor(
private _template: TemplateRef<any>,
private _componentFactoryResolver: ComponentFactoryResolver,
Expand Down Expand Up @@ -60,6 +64,7 @@ export class MatMenuContent implements OnDestroy {
// risk it staying attached to a pane that's no longer in the DOM.
element.parentNode!.insertBefore(this._outlet.outletElement, element);
this._portal.attach(this._outlet, context);
this._attached.next();
}

/**
Expand Down
11 changes: 8 additions & 3 deletions src/lib/menu/menu-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,14 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
if (menu.lazyContent) {
// Wait for the exit animation to finish before detaching the content.
menu._animationDone
.pipe(filter(event => event.toState === 'void'), take(1))
.subscribe(() => {
menu.lazyContent!.detach();
.pipe(
filter(event => event.toState === 'void'),
take(1),
// Interrupt if the content got re-attached.
takeUntil(menu.lazyContent._attached)
)
.subscribe(() => menu.lazyContent!.detach(), undefined, () => {
// No matter whether the content got re-attached, reset the menu.
this._resetMenu();
});
} else {
Expand Down