34

I'm working on a POC app, and I'm trying to get the MdDialog component working. Does anyone have a working example of what to pass to the MdDialog open method?

Angular 2.0: https://github.com/angular/angular

Angular 2 Material: https://github.com/angular/material2

Roham Rafii
  • 2,929
  • 7
  • 35
  • 49
Earl Ferguson
  • 938
  • 2
  • 10
  • 12

1 Answers1

68

Update to angular v4 and @angular/material 2.0.0-beta.12

md prefix was changed to mat

Import MatDialogModule instead of MdDialogModule

@angular/material now depends on @angular/cdk as a peer dependency.

Recap: Plunkr

8 steps to Material Dialog + Appendix

Step 1: Install package

npm i --save @angular/material @angular/cdk @angular/animations

Step 2: Configure systemjs.config.js

map: {
  ...
  '@angular/animations': 'npm:@angular/animations/bundles/animations.umd.js',
  '@angular/animations/browser': 'npm:@angular/animations/bundles/animations-browser.umd.js',
  '@angular/platform-browser/animations': 'npm:@angular/platform-browser/bundles/platform-browser-animations.umd.js',
  '@angular/material': 'npm:@angular/material/bundles/material.umd.js',
  '@angular/cdk': 'https://unpkg.com/@angular/cdk/bundles/cdk.umd.js',
  '@angular/cdk/a11y': 'https://unpkg.com/@angular/cdk/bundles/cdk-a11y.umd.js',
  '@angular/cdk/bidi': 'https://unpkg.com/@angular/cdk/bundles/cdk-bidi.umd.js',
  '@angular/cdk/coercion': 'https://unpkg.com/@angular/cdk/bundles/cdk-coercion.umd.js',
  '@angular/cdk/collections': 'https://unpkg.com/@angular/cdk/bundles/cdk-collections.umd.js',
  '@angular/cdk/keycodes': 'https://unpkg.com/@angular/cdk/bundles/cdk-keycodes.umd.js',
  '@angular/cdk/observers': 'https://unpkg.com/@angular/cdk/bundles/cdk-observers.umd.js',
  '@angular/cdk/overlay': 'https://unpkg.com/@angular/cdk/bundles/cdk-overlay.umd.js',
  '@angular/cdk/platform': 'https://unpkg.com/@angular/cdk/bundles/cdk-platform.umd.js',
  '@angular/cdk/portal': 'https://unpkg.com/@angular/cdk/bundles/cdk-portal.umd.js',
  '@angular/cdk/rxjs': 'https://unpkg.com/@angular/cdk/bundles/cdk-rxjs.umd.js',
  '@angular/cdk/scrolling': 'https://unpkg.com/@angular/cdk/bundles/cdk-scrolling.umd.js',
  '@angular/cdk/table': 'https://unpkg.com/@angular/cdk/bundles/cdk-table.umd.js',
  '@angular/cdk/stepper': 'https://unpkg.com/@angular/cdk/bundles/cdk-stepper.umd.js',
},

Step 3: Import MatDialogModule to your module

import { MatDialogModule } from '@angular/material';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

@NgModule({
  imports: [ 
    BrowserModule,
    BrowserAnimationsModule, <== required
    MatDialogModule <== here
  ],
  declarations: [ AppComponent],
  bootstrap: [ AppComponent ]
})
export class AppModule {}

Step 4: Create desired dialog component like:

@Component({
  selector: 'your-dialog-selector',
  template: `
  <h2>Hi! I am modal dialog!</h2>
  <button mat-raised-button (click)="dialogRef.close()">Close dialog</button>`
})
export class DialogComponent {
  constructor(public dialogRef: MdDialogRef<DialogComponent>) { }
}

Step 5: Add the component from step 4 to declarations and entryComponents arrays of your NgModule decorator:

@NgModule({
  imports: [ BrowserModule, MatDialogModule ],
  declarations: [ AppComponent, DialogComponent ],
  entryComponents: [ DialogComponent ],
  bootstrap: [ AppComponent ]
})
export class AppModule {}

Step 6: Use it in your component:

@Component({
  selector: 'my-app',
  template: `<button mat-raised-button (click)="openDialog()">Open dialog</button>`,
})
export class App {

  constructor(public dialog: MatDialog) { }

  openDialog(key) {
    let dialogRef = this.dialog.open(DialogComponent);
  }
}

Step 7 Pick out the desired theme:

<link href="https://unpkg.com/@angular/material/prebuilt-themes/deeppurple-amber.css" 
  rel="stylesheet">

Other themes you can find here

Step 8

If you want to pass data to modal then use the following (Plunker):

dialogRef.componentInstance.param1 = "test value";

Appendix

Routed Dialog: Plunkr

Draggable Dialog (How can i make a MatDialog draggable / Angular Material)


Plunker Example

See also

yurzui
  • 205,937
  • 32
  • 433
  • 399
  • Thanks, this saved me a lot of time. I found I also had to add the dialog to 'declarations' in the NgModule for it to work (the plunker does that as well). Also you can send a result into dialogRef.close() and access it in the observable subscription function. Which is useful for confirmation dialogs, etc. – Robert Di Paolo Oct 24 '16 at 10:55
  • Can i pass value to the Dialog Component as well? – Sumit Agarwal Nov 29 '16 at 11:49
  • @Sumit Agarwal see this question http://stackoverflow.com/questions/40826073/pass-parameter-to-mddialog-in-angular-material-2 – yurzui Nov 29 '16 at 11:55
  • Can we avoid the usage of a service class? – Sumit Agarwal Nov 29 '16 at 11:59
  • 2
    @Sumit Agarwal https://plnkr.co/edit/n185tQbEHg1xGlNCAdxQ?p=preview See line `this.dialogRef.componentInstance.param1 = "test value";` – yurzui Nov 29 '16 at 12:05
  • Hello, I followed all your above steps but getting this error. Failed to load resource: the server responded with a status of 404 (Not Found) index.html:25 Error: Fetch error: 404 Not Found ZoneDelegate.invokeTask @ zone.js:398 Zone.runTask @ zone.js:165 drainMicroTaskQueue @ zone.js:593 – S_developer Apr 05 '17 at 14:27
  • is there a way to still use reactive forms instead of `#howmuch` syntax with this? – Phil Jun 21 '17 at 19:17
  • it works, but injecting mddialogref makes this component untestable – bartosz.baczek Oct 02 '17 at 15:57
  • @bartosz.baczek Can you provide a plunker that demonstrates the issue? – yurzui Oct 02 '17 at 15:58
  • @yurzui, I'll make separate question for that. – bartosz.baczek Oct 03 '17 at 07:16
  • sorry, I'm not able to plunker it :( – bartosz.baczek Oct 03 '17 at 07:55