Skip to content

Commit c9e1844

Browse files
committed
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.
1 parent 08e9d70 commit c9e1844

File tree

6 files changed

+75
-13
lines changed

6 files changed

+75
-13
lines changed

src/demo-app/dialog/dialog-demo.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,15 @@ <h2>Other options</h2>
4646
</md-select>
4747
</p>
4848

49-
<md-checkbox [(ngModel)]="config.disableClose">Disable close</md-checkbox>
49+
<p>
50+
<md-input-container>
51+
<input mdInput [(ngModel)]="config.data.message" placeholder="Dialog message">
52+
</md-input-container>
53+
</p>
54+
55+
<p>
56+
<md-checkbox [(ngModel)]="config.disableClose">Disable close</md-checkbox>
57+
</p>
5058
</md-card-content>
5159
</md-card>
5260

src/demo-app/dialog/dialog-demo.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {Component, Inject} from '@angular/core';
22
import {DOCUMENT} from '@angular/platform-browser';
3-
import {MdDialog, MdDialogRef, MdDialogConfig} from '@angular/material';
3+
import {MdDialog, MdDialogRef, MdDialogConfig, MdDialogData} from '@angular/material';
4+
45

56
@Component({
67
moduleId: module.id,
@@ -21,6 +22,9 @@ export class DialogDemo {
2122
bottom: '',
2223
left: '',
2324
right: ''
25+
},
26+
data: {
27+
message: 'Jazzy jazz jazz'
2428
}
2529
};
2630

@@ -41,7 +45,7 @@ export class DialogDemo {
4145
openJazz() {
4246
this.dialogRef = this.dialog.open(JazzDialog, this.config);
4347

44-
this.dialogRef.afterClosed().subscribe(result => {
48+
this.dialogRef.afterClosed().subscribe((result: string) => {
4549
this.lastCloseResult = result;
4650
this.dialogRef = null;
4751
});
@@ -59,13 +63,13 @@ export class DialogDemo {
5963
template: `
6064
<p>It's Jazz!</p>
6165
<p><label>How much? <input #howMuch></label></p>
62-
<p> {{ jazzMessage }} </p>
66+
<p> {{ data.message }} </p>
6367
<button type="button" (click)="dialogRef.close(howMuch.value)">Close dialog</button>`
6468
})
6569
export class JazzDialog {
66-
jazzMessage = 'Jazzy jazz jazz';
67-
68-
constructor(public dialogRef: MdDialogRef<JazzDialog>) { }
70+
constructor(
71+
public dialogRef: MdDialogRef<JazzDialog>,
72+
public data: MdDialogData) { }
6973
}
7074

7175

@@ -104,7 +108,7 @@ export class JazzDialog {
104108
color="primary"
105109
href="https://en.wikipedia.org/wiki/Neptune"
106110
target="_blank">Read more on Wikipedia</a>
107-
111+
108112
<button
109113
md-button
110114
color="secondary"

src/lib/dialog/dialog-config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ export interface DialogPosition {
1111
right?: string;
1212
};
1313

14+
/** Data to be injected into a dialog. */
15+
export class MdDialogData {
16+
[key: string]: any;
17+
}
1418

1519
/**
1620
* Configuration for opening a modal dialog with the MdDialog service.
@@ -33,5 +37,8 @@ export class MdDialogConfig {
3337
/** Position overrides. */
3438
position?: DialogPosition;
3539

40+
/** Data being injected into the child component. */
41+
data?: MdDialogData;
42+
3643
// TODO(jelbourn): add configuration for lifecycle hooks, ARIA labelling.
3744
}

src/lib/dialog/dialog-injector.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
import {Injector} from '@angular/core';
22
import {MdDialogRef} from './dialog-ref';
3+
import {MdDialogData} from './dialog-config';
34

45

56
/** Custom injector type specifically for instantiating components with a dialog. */
67
export class DialogInjector implements Injector {
7-
constructor(private _dialogRef: MdDialogRef<any>, private _parentInjector: Injector) { }
8+
constructor(
9+
private _dialogRef: MdDialogRef<any>,
10+
private _data: MdDialogData,
11+
private _parentInjector: Injector) { }
812

913
get(token: any, notFoundValue?: any): any {
1014
if (token === MdDialogRef) {
1115
return this._dialogRef;
1216
}
1317

18+
if (token === MdDialogData && this._data) {
19+
return this._data;
20+
}
21+
1422
return this._parentInjector.get(token, notFoundValue);
1523
}
1624
}

src/lib/dialog/dialog.spec.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {MdDialog} from './dialog';
1414
import {OverlayContainer} from '../core';
1515
import {MdDialogRef} from './dialog-ref';
1616
import {MdDialogContainer} from './dialog-container';
17+
import {MdDialogData} from './dialog-config';
1718

1819

1920
describe('MdDialog', () => {
@@ -271,6 +272,28 @@ describe('MdDialog', () => {
271272
expect(overlayContainerElement.querySelectorAll('md-dialog-container').length).toBe(0);
272273
});
273274

275+
describe('passing in data', () => {
276+
it('should be able to pass in data', () => {
277+
let config = {
278+
data: {
279+
stringParam: 'hello',
280+
dateParam: new Date()
281+
}
282+
};
283+
284+
let instance = dialog.open(DialogWithInjectedData, config).componentInstance;
285+
286+
expect(instance.data['stringParam']).toBe(config.data.stringParam);
287+
expect(instance.data['dateParam']).toBe(config.data.dateParam);
288+
});
289+
290+
it('should throw if injected data is expected but none is passed', () => {
291+
expect(() => {
292+
dialog.open(DialogWithInjectedData);
293+
}).toThrow();
294+
});
295+
});
296+
274297
describe('disableClose option', () => {
275298
it('should prevent closing via clicks on the backdrop', () => {
276299
dialog.open(PizzaMsg, {
@@ -505,19 +528,31 @@ class ComponentThatProvidesMdDialog {
505528
constructor(public dialog: MdDialog) {}
506529
}
507530

531+
/** Simple component for testing ComponentPortal. */
532+
@Component({template: ''})
533+
class DialogWithInjectedData {
534+
constructor(public data: MdDialogData) { }
535+
}
536+
508537
// Create a real (non-test) NgModule as a workaround for
509538
// https://github.com/angular/angular/issues/10760
510539
const TEST_DIRECTIVES = [
511540
ComponentWithChildViewContainer,
512541
PizzaMsg,
513542
DirectiveWithViewContainer,
514-
ContentElementDialog
543+
ContentElementDialog,
544+
DialogWithInjectedData
515545
];
516546

517547
@NgModule({
518548
imports: [MdDialogModule],
519549
exports: TEST_DIRECTIVES,
520550
declarations: TEST_DIRECTIVES,
521-
entryComponents: [ComponentWithChildViewContainer, PizzaMsg, ContentElementDialog],
551+
entryComponents: [
552+
ComponentWithChildViewContainer,
553+
PizzaMsg,
554+
ContentElementDialog,
555+
DialogWithInjectedData
556+
],
522557
})
523558
class DialogTestModule { }

src/lib/dialog/dialog.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export class MdDialog {
129129
// to modify and close it.
130130
let dialogRef = <MdDialogRef<T>> new MdDialogRef(overlayRef);
131131

132-
if (!dialogContainer.dialogConfig.disableClose) {
132+
if (!config.disableClose) {
133133
// When the dialog backdrop is clicked, we want to close it.
134134
overlayRef.backdropClick().first().subscribe(() => dialogRef.close());
135135
}
@@ -141,7 +141,7 @@ export class MdDialog {
141141
// inject the MdDialogRef. This allows a component loaded inside of a dialog to close itself
142142
// and, optionally, to return a value.
143143
let userInjector = config && config.viewContainerRef && config.viewContainerRef.injector;
144-
let dialogInjector = new DialogInjector(dialogRef, userInjector || this._injector);
144+
let dialogInjector = new DialogInjector(dialogRef, config.data, userInjector || this._injector);
145145

146146
let contentPortal = new ComponentPortal(component, null, dialogInjector);
147147

0 commit comments

Comments
 (0)