Angular Material - Initialize md dialog with data passed from parent component in angular material2 - Angular Material Tutorial
Initialize md dialog with data passed from parent component in angular material2
This example requires MdDialogRef and MD_DIALOG_DATA. Please import them in the component module.
import {MdDialog, MdDialogRef, MD_DIALOG_DATA} from '@angular/material';
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy angular material tutorial , angular 4 material , angular material2 , angular material example team
input-overview-example.html:
import {MdDialog, MdDialogRef, MD_DIALOG_DATA} from '@angular/material';
<md-input-container>
<input mdInput
[(ngModel)]="id"
placeholder="Value passed to md-dialog">
</md-input-container>
<p></p>
<button md-raised-button
(click)="openDialog(id)">
Open Dialog
</button>
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy angular material tutorial , angular 4 material , angular material2 , angular material example team
input-overview-example.ts:
import {MdDialog, MdDialogRef, MD_DIALOG_DATA} from '@angular/material';
<md-input-container>
<input mdInput
[(ngModel)]="id"
placeholder="Value passed to md-dialog">
</md-input-container>
<p></p>
<button md-raised-button
(click)="openDialog(id)">
Open Dialog
</button>
constructor(public dialog: MdDialog) {}
openDialog(value) {
let dialogRef = this.dialog.open(DialogResultExampleDialog, {
data: {
id: value
}
});
dialogRef.afterClosed().subscribe(result => {
console.log(result);
});
}
}
@Component({
selector: 'dialog-result-example-dialog',
template: `<p md-dialog-title>Confirm Toggle </p>
<p md-dialog-content>Id passed from component: {{ this.passedId }}</p>
<md-dialog-actions>
<button md-button color="primary" (click)="dialogRef.close('Cancel')">Cancel</button>
<button md-button color="primary" (click)="dialogRef.close('continue')">Continue</button>
</md-dialog-actions>
`,
})
export class DialogResultExampleDialog implements OnInit {
passedId: string;
constructor(@Inject(MD_DIALOG_DATA) private data: { id: string },
public dialogRef: MdDialogRef<DialogResultExampleDialog>) {}
ngOnInit(){
this.passedId = this.data.id;
}
}