close material dialog from different component angular

纵饮孤独 提交于 2019-12-13 09:51:21

问题


I have component A which openes material dialog

component A

  openDialog(): void {
    const dialogRef = this.dialog.open(**component B**, {
      width: '1000px',
    });

then I use to load image in that dialog and with button upload I upload it using component B to the server. Now when I've finished uploading image I want to close that dialog from component B.

component B

onUpload() {
  const fd = new FormData(); 
  fd.append('image', this.selectedFile);
  this.service.uploadImage(fd).subscribe(
    (res:any)=>
    {
//how to close dialog here ?

    },

how can I do that ?


回答1:


Dialog | Angular Material Documentation:

Components created via MatDialog can inject MatDialogRef and use it to close the dialog in which they are contained. When closing, an optional result value can be provided. This result value is forwarded as the result of the afterClosed promise

export class ComponentB {
  constructor(
    public dialogRef: MatDialogRef<DialogOverviewExampleDialog>
  ) {}

  onUpload(): void {
    // upload stuff

    this.dialogRef.close();
  }
}


来源:https://stackoverflow.com/questions/57822013/close-material-dialog-from-different-component-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!