@@ -59,7 +59,7 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
59
59
@ViewChild ( FocusTrap ) _focusTrap : FocusTrap ;
60
60
61
61
/** Element that was focused before the dialog was opened. Save this to restore upon close. */
62
- private _elementFocusedBeforeDialogWasOpened : Element = null ;
62
+ private _elementFocusedBeforeDialogWasOpened : HTMLElement = null ;
63
63
64
64
/** The dialog configuration. */
65
65
dialogConfig : MdDialogConfig ;
@@ -89,7 +89,7 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
89
89
// ready in instances where change detection has to run first. To deal with this, we simply
90
90
// wait for the microtask queue to be empty.
91
91
this . _ngZone . onMicrotaskEmpty . first ( ) . subscribe ( ( ) => {
92
- this . _elementFocusedBeforeDialogWasOpened = document . activeElement ;
92
+ this . _elementFocusedBeforeDialogWasOpened = document . activeElement as HTMLElement ;
93
93
this . _focusTrap . focusFirstTabbableElement ( ) ;
94
94
} ) ;
95
95
@@ -106,7 +106,14 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
106
106
// the dialog was opened. Wait for the DOM to finish settling before changing the focus so
107
107
// that it doesn't end up back on the <body>.
108
108
this . _ngZone . onMicrotaskEmpty . first ( ) . subscribe ( ( ) => {
109
- ( this . _elementFocusedBeforeDialogWasOpened as HTMLElement ) . focus ( ) ;
109
+ let toFocus = this . _elementFocusedBeforeDialogWasOpened ;
110
+
111
+ // We need to check whether the focus method exists at all, because IE seems to throw an
112
+ // exception, even if the element is the document.body.
113
+ if ( toFocus && 'focus' in toFocus ) {
114
+ toFocus . focus ( ) ;
115
+ }
116
+
110
117
this . _onAnimationStateChange . complete ( ) ;
111
118
} ) ;
112
119
}
0 commit comments