@@ -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,9 +95,11 @@ 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 ( ) )
102+ return this . _overlay . create ( this . _getOverlayConfig ( ) )
99103 . then ( overlay => this . _overlayRef = overlay ) ;
100104 }
101105
0 commit comments