From c9e1844596dd09ed38ac890c4e52432fad658de0 Mon Sep 17 00:00:00 2001 From: crisbeto Date: Sun, 18 Dec 2016 15:25:49 +0200 Subject: [PATCH] feat(dialog): add a config option for passing in data Adds the ability to pass in data to a dialog via the `data` property. While this was previously possible through the `dialogRef.componentInstance.someUserSuppliedProperty`, it wasn't the most intuitive. The new approach looks like this: ``` dialog.open(SomeDialog, { data: { hello: 'world' } }); class SometDialog { constructor(data: MdDialogData) { console.log(data['hello']); } } ``` Fixes #2181. --- src/demo-app/dialog/dialog-demo.html | 10 ++++++- src/demo-app/dialog/dialog-demo.ts | 18 ++++++++----- src/lib/dialog/dialog-config.ts | 7 +++++ src/lib/dialog/dialog-injector.ts | 10 ++++++- src/lib/dialog/dialog.spec.ts | 39 ++++++++++++++++++++++++++-- src/lib/dialog/dialog.ts | 4 +-- 6 files changed, 75 insertions(+), 13 deletions(-) diff --git a/src/demo-app/dialog/dialog-demo.html b/src/demo-app/dialog/dialog-demo.html index 53734bbbfdff..0391728e7cca 100644 --- a/src/demo-app/dialog/dialog-demo.html +++ b/src/demo-app/dialog/dialog-demo.html @@ -46,7 +46,15 @@

Other options

- Disable close +

+ + + +

+ +

+ Disable close +

diff --git a/src/demo-app/dialog/dialog-demo.ts b/src/demo-app/dialog/dialog-demo.ts index 5e91c097f9aa..c7109a8ebebd 100644 --- a/src/demo-app/dialog/dialog-demo.ts +++ b/src/demo-app/dialog/dialog-demo.ts @@ -1,6 +1,7 @@ import {Component, Inject} from '@angular/core'; import {DOCUMENT} from '@angular/platform-browser'; -import {MdDialog, MdDialogRef, MdDialogConfig} from '@angular/material'; +import {MdDialog, MdDialogRef, MdDialogConfig, MdDialogData} from '@angular/material'; + @Component({ moduleId: module.id, @@ -21,6 +22,9 @@ export class DialogDemo { bottom: '', left: '', right: '' + }, + data: { + message: 'Jazzy jazz jazz' } }; @@ -41,7 +45,7 @@ export class DialogDemo { openJazz() { this.dialogRef = this.dialog.open(JazzDialog, this.config); - this.dialogRef.afterClosed().subscribe(result => { + this.dialogRef.afterClosed().subscribe((result: string) => { this.lastCloseResult = result; this.dialogRef = null; }); @@ -59,13 +63,13 @@ export class DialogDemo { template: `

It's Jazz!

-

{{ jazzMessage }}

+

{{ data.message }}

` }) export class JazzDialog { - jazzMessage = 'Jazzy jazz jazz'; - - constructor(public dialogRef: MdDialogRef) { } + constructor( + public dialogRef: MdDialogRef, + public data: MdDialogData) { } } @@ -104,7 +108,7 @@ export class JazzDialog { color="primary" href="https://en.wikipedia.org/wiki/Neptune" target="_blank">Read more on Wikipedia - +