Reputation: 783
I am developping an angular application. I try to use MatDialog to open a dialog box and try to be able to close it
I open the dialog this way
openDialog(event) {
const dialogConfig = new MatDialogConfig();
dialogConfig.disableClose = true;
dialogConfig.autoFocus = true;
dialogConfig.position = {
top: bottom + 'px',
right: '0px'
};
dialogConfig.width = '50%';
dialogConfig.height = '590px';
this.dialog.open(UserDialogComponent, dialogConfig);
const dialogRef = this.dialog.open(UserDialogComponent, dialogConfig);
dialogRef.beforeClose().subscribe((result: string) => {
console.log('RIght before close,', result);
});
dialogRef.afterClosed().subscribe(result => {
console.log('The dialog was closed', result);
});
}
I inject MatDialogRef in the constructor of UserDialogComponent:
constructor(
private formBuilder: FormBuilder,
private dialogRef: MatDialogRef<UserDialogComponent>,
@Inject(MAT_DIALOG_DATA) data) {
console.log("Constructor UserDialogComponent START");
console.log(dialogRef);
this.dialogRef = dialogRef;
console.log("Constructor UserDialogComponent END");
}
And to close the dialog box, I use this function
close() {
console.log(this.dialogRef);
console.log('CLOSE CLICKED');
this.dialogRef.close(true);
}
But this.dialogRef is an empty object and I receive the following error when I call this function
ERROR TypeError: "this.dialogRef.close is not a function"
Could you help me ?
Upvotes: 1
Views: 2674
Reputation: 783
you put me on the trail
There was this code
@Component({
selector: 'app-user-dialog',
templateUrl: './user-dialog.component.html',
styleUrls: ['./user-dialog.component.scss'],
providers: [
{provide: MAT_RADIO_DEFAULT_OPTIONS, useValue: { color: 'accent' }},
{ provide: MatDialogRef, useValue: {} }
]
})
export class UserDialogComponent implements OnInit {
I removed
{ provide: MatDialogRef, useValue: {} }
And now it works fine
Upvotes: 1
Reputation: 931
remove this this.dialogRef = dialogRef;
and try
@Inject(MAT_DIALOG_DATA) data) {
console.log("Constructor UserDialogComponent START");
console.log(dialogRef);
this.dialogRef = dialogRef; // delete this line
console.log("Constructor UserDialogComponent END");
}
Upvotes: 0