1

I want to pass data to a dialog but the way it should be done (according to several examples found on the internet, like Using MdDialogConfig data on Angular 2) does not work.

I get the following error:

 Property 'data' does not exist on type 'MdDialogConfig'

When I look at the code of the MdDialogConfig this property indeed does not exists (anymore)

I used the example that can be found as the accepted answer to the topic above, but it doesn't work. The example code being:

const config = new MdDialogConfig();

config.data = [
   // for example:
   'value 1',
   'value 2'
];

const dialogRef = this.dialog.open(DialogComponent, config);

Scanning the rest of the code, I see no other way to pass data directly. I could fall back to using a separate service to pass data from the component to the dialog but that is far from ideal.

Is there a proper way to pass data to a dialog?

I am using version 2.0.0-beta.1

Community
  • 1
  • 1
Walter Brand
  • 679
  • 3
  • 9

1 Answers1

0

You can create your own inflatable Component (note the detail field)

export class DialogDetailComponent implements OnInit {

    @Input() detail: Detail;

    constructor(public dialogRef: MdDialogRef<DialogDetailComponent>) {
    }

    ngOnInit() {
    }

    close() {
        this.dialogRef.close();
    }

}
export interface Detail { }

And create a method that opens that dialog in your Service

openDialogDetail(c: Type<DialogDetailComponent>, detail: Detail, afterClosed?: (data?) => any, type?: number, width: string = "80%", height: string = "80%", disableClose = false) {

    let dialogRef = this.openDialog(c, afterClosed, width, height, disableClose);

    if (detail)
        dialogRef.componentInstance.detail = detail;

    return dialogRef;

}