In the parent component:
const config = new MdDialogConfig(); config.data = [ // for example: 'value 1', 'value 2' ]; const dialogRef = this.dialog.open(DialogComponent, config);
DialogComponent :
import { Component, OnInit } from '@angular/core'; import { MdDialogRef } from '@angular/material'; @Component({ selector: 'dialog', template: require('./dialog.component.pug'), styleUrls: [ './dialog.component.scss' ] }) export class DialogComponent implements OnInit { private values; constructor(public dialogRef: MdDialogRef<DialogComponent>) {} ngOnInit(): void { this.values = this.dialogRef.config.data; } }
And a sample template (HTML version):
<md-dialog-content> <md-list *ngIf="values"> <md-list-item *ngFor="let value of values">{{ value }}</md-list-item> </md-list> </md-dialog-content>
Update - @angular/material 2.0.0-beta.3
Since version 2.0.0-beta.3 is from Angular material, access to the config (and config.data ) property is MdDialogRef<T> . Instead, you must enter the MD_DIALOG_DATA token to access any data transferred to your dialog component.
Import:
import { Component, Inject, OnInit, Optional } from '@angular/core'; import { MD_DIALOG_DATA, MdDialogRef } from '@angular/material';
Constructor:
constructor( @Optional() @Inject(MD_DIALOG_DATA) private dialogData: any, private dialogRef: MdDialogRef<DialogComponent> ) {}
ngOnInit method:
ngOnInit(): void { // alternatively, rename 'dialogData' to 'values' and remove the // 'ngOnInit' method this.values = this.dialogData; }
In many cases, you will need to support the @Optional() decorator until the 4086 problem is resolved.
source share