Here's a simplified guide to using dialogs effectively:
Begin by opening a dialog with relevant options and providing your own data (such as an ID).
this._dialog.open(MyEditDialogComponent, {
maxHeight: '90%',
maxWidth: '90%',
...etc,
data: {id: 123}
});
I wrapped the data in an object, but if you only need an ID, it's not necessary (I tend to use objects for everything these days).
Next step is to utilize the data in your dialog component:
export class MyEditDialogComponent implements OnInit {
constructor(@Inject(MAT_DIALOG_DATA) public data: {id: number}) { }
public ngOnInit(): void {
// access the id value...
const id = this.data.id;
}
}
If you're only passing an ID, I assume there's a service where the dialog can retrieve its own data for populating the edit fields.
Alternatively, the dialog could be used purely for editing UI elements if the main page already has all the necessary data displayed.
In that case, the parent component can handle the communication with backend services.
To manage outputs from dialogs, start by emitting data when closing them (e.g. upon clicking a save button):
export class MyEditDialogComponent implements OnInit {
constructor(@Inject(MAT_DIALOG_DATA) public data: {id: number},
private readonly dialogRef: MatDialogRef<MyEditDialogComponent>) {
}
public onCloseClicked(): void {
this.dialogRef.close({some: 'data'});
}
}
Then, the parent component can process the output:
const sub = this._dialog.open(MyEditDialogComponent, {
maxHeight: '90%',
maxWidth: '90%',
...etc,
data: {id: 123}
})
.subscribe((result: {some: string}) => {
sub.unsubscribe();
// do something with the output
const outputData = result.some;
});