@@ -47,7 +47,6 @@ export class MdMenuTrigger implements AfterViewInit, OnDestroy {
47
47
48
48
ngAfterViewInit ( ) {
49
49
this . _checkMenu ( ) ;
50
- this . _createOverlay ( ) ;
51
50
this . menu . close . subscribe ( ( ) => this . closeMenu ( ) ) ;
52
51
}
53
52
@@ -59,11 +58,14 @@ export class MdMenuTrigger implements AfterViewInit, OnDestroy {
59
58
}
60
59
61
60
openMenu ( ) : Promise < void > {
62
- return this . _overlayRef . attach ( this . _portal )
61
+ return this . _createOverlay ( )
62
+ . then ( ( ) => this . _overlayRef . attach ( this . _portal ) )
63
63
. then ( ( ) => this . _setIsMenuOpen ( true ) ) ;
64
64
}
65
65
66
66
closeMenu ( ) : Promise < void > {
67
+ if ( ! this . _overlayRef ) return Promise . resolve ( ) ;
68
+
67
69
return this . _overlayRef . detach ( )
68
70
. then ( ( ) => this . _setIsMenuOpen ( false ) ) ;
69
71
}
@@ -93,10 +95,12 @@ export class MdMenuTrigger implements AfterViewInit, OnDestroy {
93
95
* This method creates the overlay from the provided menu's template and saves its
94
96
* OverlayRef so that it can be attached to the DOM when openMenu is called.
95
97
*/
96
- private _createOverlay ( ) : void {
98
+ private _createOverlay ( ) : Promise < any > {
99
+ if ( this . _overlayRef ) { return Promise . resolve ( ) ; }
100
+
97
101
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 )
100
104
}
101
105
102
106
/**
0 commit comments