Skip to content

Commit a2d348a

Browse files
committed
lazy create menu
1 parent 29ad153 commit a2d348a

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/components/menu/menu-trigger.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ export class MdMenuTrigger implements AfterViewInit, OnDestroy {
4747

4848
ngAfterViewInit() {
4949
this._checkMenu();
50-
this._createOverlay();
5150
this.menu.close.subscribe(() => this.closeMenu());
5251
}
5352

@@ -59,11 +58,14 @@ export class MdMenuTrigger implements AfterViewInit, OnDestroy {
5958
}
6059

6160
openMenu(): Promise<void> {
62-
return this._overlayRef.attach(this._portal)
61+
return this._createOverlay()
62+
.then(() => this._overlayRef.attach(this._portal))
6363
.then(() => this._setIsMenuOpen(true));
6464
}
6565

6666
closeMenu(): Promise<void> {
67+
if (!this._overlayRef) return Promise.resolve();
68+
6769
return this._overlayRef.detach()
6870
.then(() => this._setIsMenuOpen(false));
6971
}
@@ -93,10 +95,12 @@ export class MdMenuTrigger implements AfterViewInit, OnDestroy {
9395
* This method creates the overlay from the provided menu's template and saves its
9496
* OverlayRef so that it can be attached to the DOM when openMenu is called.
9597
*/
96-
private _createOverlay(): void {
98+
private _createOverlay(): Promise<any> {
99+
if (this._overlayRef) { return Promise.resolve(); }
100+
97101
this._portal = new TemplatePortal(this.menu.templateRef, this._viewContainerRef);
98-
this._overlay.create(this._getOverlayConfig())
99-
.then(overlay => this._overlayRef = overlay);
102+
return this._overlay.create(this._getOverlayConfig())
103+
.then(overlay => this._overlayRef = overlay)
100104
}
101105

102106
/**

0 commit comments

Comments
 (0)