I'm trying to use a dialog component in Angular 2 using @angular/material2.0.0-beta.1
. What I'm trying to accomplish is to send data (which are values that a person chooses from the interface, the dialog is used to make the person confirm the values they chose) to the dialog and display it. So for example the dialog should say something like this:
You chose:
option 1: value
option 2: value
option 3: value
Cancel | Confirm
How can I pass these values to the dialog I create so that I can just access them like so {{value}} in the view template? I think its using the data config, but I can't seem to find good documentation or examples on how to use it. Here's what I've been trying:
let config = new MdDialogConfig().data();
let dialogRef = this.dialog.open(DialogComponent);
DialogComponent
import { Component } from '@angular/core';
import { MdDialogRef } from '@angular/material';
@Component({
selector: 'dialog',
template: require('./dialog.component.pug'),
styleUrls: [
'./dialog.component.scss'
]
})
export class DialogComponent {
constructor(public dialogRef: MdDialogRef<DialogComponent>) {}
}
Another way to do is to set the values from within the parent component DialogComponent
ParentComponent
Update: Definitely use the above Inject method to get your data. This way is dependent upon accessing a private member which is not guaranteed to remain untouched between versions
If you're using the new 2.0.0 beta 3 (or maybe it was even changed in beta 2, not sure) then the above answer has to be changed a little bit:
It seems like there should be a better way of referencing the data than
this.dialogRef._containerInstance.dialogConfig.data
but I couldn't find oneIn parent component:
DialogComponent
:And sample template (HTML version):
Update —
@angular/material
2.0.0-beta.3Since version 2.0.0-beta.3 of Angular Material, it is no longer possible to access
config
(andconfig.data
) property ofMdDialogRef<T>
. Instead, you should injectMD_DIALOG_DATA
token to access any data passed into your dialog component.Imports:
The constructor:
ngOnInit
method:In many cases you’ll need to keep the
@Optional()
decorator, until issue 4086 will get resolved.