|
| 1 | +# MdDialog |
| 2 | + |
| 3 | +MdDialog is a service, which opens dialogs components in the view. |
| 4 | + |
| 5 | +### Methods |
| 6 | + |
| 7 | +| Name | Description | |
| 8 | +| --- | --- | |
| 9 | +| `open(component: ComponentType<T>, config: MdDialogConfig): MdDialogRef<T>` | Creates and opens a dialog matching material spec. | |
| 10 | + |
| 11 | +### Config |
| 12 | + |
| 13 | +| Key | Description | |
| 14 | +| --- | --- | |
| 15 | +| `viewContainerRef: ViewContainerRef` | The view container ref to attach the dialog to. | |
| 16 | +| `role: DialogRole = 'dialog'` | The ARIA role of the dialog element. Possible values are `dialog` and `alertdialog`. Defaults to `dialog`. | |
| 17 | + |
| 18 | +## MdDialogRef |
| 19 | + |
| 20 | +A reference to the dialog created by the MdDialog `open` method. |
| 21 | + |
| 22 | +### Methods |
| 23 | + |
| 24 | +| Name | Description | |
| 25 | +| --- | --- | |
| 26 | +| `close(dialogResult?: any)` | Closes the dialog, pushing a value to the afterClosed observable. | |
| 27 | +| `afterClosed(): Observable<any>` | Returns an observable which will emit the dialog result, passed to the `close` method above. | |
| 28 | + |
| 29 | +## Example |
| 30 | +The service can be injected in a component. |
| 31 | + |
| 32 | +```ts |
| 33 | +@Component({ |
| 34 | + selector: 'pizza-component', |
| 35 | + template: ` |
| 36 | + <button type="button" (click)="openDialog()">Open dialog</button> |
| 37 | + ` |
| 38 | +}) |
| 39 | +export class PizzaComponent { |
| 40 | + |
| 41 | + dialogRef: MdDialogRef<PizzaDialog>; |
| 42 | + |
| 43 | + constructor( |
| 44 | + public dialog: MdDialog, |
| 45 | + public viewContainerRef: ViewContainerRef) { } |
| 46 | + |
| 47 | + openDialog() { |
| 48 | + let config = new MdDialogConfig(); |
| 49 | + config.viewContainerRef = this.viewContainerRef; |
| 50 | + |
| 51 | + this.dialogRef = this.dialog.open(PizzaDialog, config); |
| 52 | + |
| 53 | + this.dialogRef.afterClosed().subscribe(result => { |
| 54 | + console.log('result: ' + result); |
| 55 | + this.dialogRef = null; |
| 56 | + }); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +@Component({ |
| 61 | + selector: 'pizza-dialog', |
| 62 | + template: ` |
| 63 | + <button type="button" (click)="dialogRef.close('yes')">Yes</button> |
| 64 | + <button type="button" (click)="dialogRef.close('no')">No</button> |
| 65 | + ` |
| 66 | +}) |
| 67 | +export class PizzaDialog { |
| 68 | + constructor(public dialogRef: MdDialogRef<PizzaDialog>) { } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +The dialog component should be declared in the list of entry components of the module: |
| 73 | + |
| 74 | +```ts |
| 75 | +@NgModule({ |
| 76 | + declarations: [ |
| 77 | + ..., |
| 78 | + PizzaDialog |
| 79 | + ], |
| 80 | + entryComponents: [ |
| 81 | + ..., |
| 82 | + PizzaDialog |
| 83 | + ], |
| 84 | + ... |
| 85 | +}) |
| 86 | +export class AppModule { } |
| 87 | + |
| 88 | +``` |
0 commit comments