1- import { Injector , ComponentRef , Injectable , Optional , SkipSelf } from '@angular/core' ;
1+ import { Injector , ComponentRef , Injectable , Optional , SkipSelf , TemplateRef } from '@angular/core' ;
22import { Observable } from 'rxjs/Observable' ;
33import { Subject } from 'rxjs/Subject' ;
4-
54import { Overlay , OverlayRef , ComponentType , OverlayState , ComponentPortal } from '../core' ;
65import { extendObject } from '../core/util/object-extend' ;
7-
86import { DialogInjector } from './dialog-injector' ;
97import { MdDialogConfig } from './dialog-config' ;
108import { MdDialogRef } from './dialog-ref' ;
119import { MdDialogContainer } from './dialog-container' ;
10+ import { TemplatePortal } from '../core/portal/portal' ;
1211
1312
14- // TODO(jelbourn): add support for opening with a TemplateRef
1513// TODO(jelbourn): animations
1614
1715
@@ -53,16 +51,19 @@ export class MdDialog {
5351
5452 /**
5553 * Opens a modal dialog containing the given component.
56- * @param component Type of the component to load into the load.
54+ * @param componentOrTemplateRef Type of the component to load into the dialog,
55+ * or a TemplateRef to instantiate as the dialog content.
5756 * @param config Extra configuration options.
5857 * @returns Reference to the newly-opened dialog.
5958 */
60- open < T > ( component : ComponentType < T > , config ?: MdDialogConfig ) : MdDialogRef < T > {
59+ open < T > ( componentOrTemplateRef : ComponentType < T > | TemplateRef < T > ,
60+ config ?: MdDialogConfig ) : MdDialogRef < T > {
6161 config = _applyConfigDefaults ( config ) ;
6262
6363 let overlayRef = this . _createOverlay ( config ) ;
6464 let dialogContainer = this . _attachDialogContainer ( overlayRef , config ) ;
65- let dialogRef = this . _attachDialogContent ( component , dialogContainer , overlayRef , config ) ;
65+ let dialogRef =
66+ this . _attachDialogContent ( componentOrTemplateRef , dialogContainer , overlayRef , config ) ;
6667
6768 this . _openDialogs . push ( dialogRef ) ;
6869 dialogRef . afterClosed ( ) . subscribe ( ( ) => this . _removeOpenDialog ( dialogRef ) ) ;
@@ -114,14 +115,15 @@ export class MdDialog {
114115
115116 /**
116117 * Attaches the user-provided component to the already-created MdDialogContainer.
117- * @param component The type of component being loaded into the dialog.
118+ * @param componentOrTemplateRef The type of component being loaded into the dialog,
119+ * or a TemplateRef to instantiate as the content.
118120 * @param dialogContainer Reference to the wrapping MdDialogContainer.
119121 * @param overlayRef Reference to the overlay in which the dialog resides.
120122 * @param config The dialog configuration.
121123 * @returns A promise resolving to the MdDialogRef that should be returned to the user.
122124 */
123125 private _attachDialogContent < T > (
124- component : ComponentType < T > ,
126+ componentOrTemplateRef : ComponentType < T > | TemplateRef < T > ,
125127 dialogContainer : MdDialogContainer ,
126128 overlayRef : OverlayRef ,
127129 config ?: MdDialogConfig ) : MdDialogRef < T > {
@@ -143,10 +145,13 @@ export class MdDialog {
143145 let userInjector = config && config . viewContainerRef && config . viewContainerRef . injector ;
144146 let dialogInjector = new DialogInjector ( userInjector || this . _injector , dialogRef , config . data ) ;
145147
146- let contentPortal = new ComponentPortal ( component , null , dialogInjector ) ;
147-
148- let contentRef = dialogContainer . attachComponentPortal ( contentPortal ) ;
149- dialogRef . componentInstance = contentRef . instance ;
148+ if ( componentOrTemplateRef instanceof TemplateRef ) {
149+ dialogContainer . attachTemplatePortal ( new TemplatePortal ( componentOrTemplateRef , null ) ) ;
150+ } else {
151+ let contentRef = dialogContainer . attachComponentPortal (
152+ new ComponentPortal ( componentOrTemplateRef , null , dialogInjector ) ) ;
153+ dialogRef . componentInstance = contentRef . instance ;
154+ }
150155
151156 return dialogRef ;
152157 }
0 commit comments