Reputation: 1180
I checked on a few post here and double checked on how to pass data from an Angular Component to MatDialog but I am getting 'undefined' when the dialog is loading.
Here is my code so far
import { MatDialog } from '@angular/material';
let dialogReff = this.dialog.open(MyComponent,
{
panelClass: 'custom-dialog-container',
data: { myObject: selectedObject},
disableClose: true
});
I added a breakpoint here and verified that selectedObject contains all the fields and not 'undefined'
In my dialog constructor
import { Component, OnInit, AfterViewInit, Inject, ViewChild, ElementRef } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
import { FormControl, Validators } from '@angular/forms';
constructor(public dialogRef: MatDialogRef<InventoryRequiredInfoViewerComponent>,
@Inject(MAT_DIALOG_DATA) public data: { myObject: any}) {
var test = data.myObject;
}
When I break here, the data.myObject is undefined.
I added the dialog class to app.module.ts sections:
@NgModule({
declarations:
and
entryComponents:
I read a few post and can't figure out why the data object is not successfully be passed to the MatDialog. Any help is appreciated.
Upvotes: 0
Views: 3868
Reputation: 341
Hi try to modify your code as follows:
let dialogConfig = new MatDialogConfig();
dialogConfig.disableClose = true;
dialogConfig.autoFocus = true;
dialogConfig.maxWidth = '400px';
dialogConfig.panelClass = 'custom-dialog-container';
dialogConfig.data = {
myObject: selectedObject,
};
let dialogReff = this.dialog.open(MyComponent, dialogConfig);
I hope I've helped you
Regards
Upvotes: 0
Reputation: 71
I think you are able to get the data but you're redefining it in the constructor. Try this: @Inject(MAT_DIALOG_DATA) data
Upvotes: 1