66 * found in the LICENSE file at https://angular.io/license
77 */
88
9- import { Injector , ComponentRef , Injectable , Optional , SkipSelf , TemplateRef } from '@angular/core' ;
9+ import {
10+ Injector ,
11+ InjectionToken ,
12+ ComponentRef ,
13+ Injectable ,
14+ Optional ,
15+ SkipSelf ,
16+ TemplateRef ,
17+ } from '@angular/core' ;
1018import { Location } from '@angular/common' ;
1119import { Observable } from 'rxjs/Observable' ;
1220import { Subject } from 'rxjs/Subject' ;
@@ -25,6 +33,8 @@ import {MdDialogRef} from './dialog-ref';
2533import { MdDialogContainer } from './dialog-container' ;
2634import { TemplatePortal } from '../core/portal/portal' ;
2735
36+ export const MD_DIALOG_DATA = new InjectionToken < any > ( 'MdDialogData' ) ;
37+
2838
2939/**
3040 * Service to open Material Design modal dialogs.
@@ -187,17 +197,12 @@ export class MdDialog {
187197 } ) ;
188198 }
189199
190- // We create an injector specifically for the component we're instantiating so that it can
191- // inject the MdDialogRef. This allows a component loaded inside of a dialog to close itself
192- // and, optionally, to return a value.
193- let userInjector = config && config . viewContainerRef && config . viewContainerRef . injector ;
194- let dialogInjector = new DialogInjector ( userInjector || this . _injector , dialogRef , config . data ) ;
195-
196200 if ( componentOrTemplateRef instanceof TemplateRef ) {
197201 dialogContainer . attachTemplatePortal ( new TemplatePortal ( componentOrTemplateRef , null ) ) ;
198202 } else {
203+ let injector = this . _createInjector < T > ( config , dialogRef , dialogContainer ) ;
199204 let contentRef = dialogContainer . attachComponentPortal (
200- new ComponentPortal ( componentOrTemplateRef , null , dialogInjector ) ) ;
205+ new ComponentPortal ( componentOrTemplateRef , null , injector ) ) ;
201206 dialogRef . componentInstance = contentRef . instance ;
202207 }
203208
@@ -208,6 +213,29 @@ export class MdDialog {
208213 return dialogRef ;
209214 }
210215
216+ /**
217+ * Creates a custom injector to be used inside the dialog. This allows a component loaded inside
218+ * of a dialog to close itself and, optionally, to return a value.
219+ * @param config Config object that is used to construct the dialog.
220+ * @param dialogRef Reference to the dialog.
221+ * @param container Dialog container element that wraps all of the contents.
222+ * @returns The custom injector that can be used inside the dialog.
223+ */
224+ private _createInjector < T > (
225+ config : MdDialogConfig ,
226+ dialogRef : MdDialogRef < T > ,
227+ dialogContainer : MdDialogContainer ) : DialogInjector {
228+
229+ let userInjector = config && config . viewContainerRef && config . viewContainerRef . injector ;
230+ let injectionTokens = new WeakMap ( ) ;
231+
232+ injectionTokens . set ( MdDialogRef , dialogRef ) ;
233+ injectionTokens . set ( MdDialogContainer , dialogContainer ) ;
234+ injectionTokens . set ( MD_DIALOG_DATA , config . data ) ;
235+
236+ return new DialogInjector ( userInjector || this . _injector , injectionTokens ) ;
237+ }
238+
211239 /**
212240 * Removes a dialog from the array of open dialogs.
213241 * @param dialogRef Dialog to be removed.
0 commit comments