【问题标题】:transfer data from one component to another angular将数据从一个组件传输到另一个角度
【发布时间】:2020-01-09 09:37:01
【问题描述】:

我有 组件 A,它会触发 MatDialog

  openDialog(): void {
   // console.log('The dialog was opened');

    const dialogRef = this.dialog.open(PicuploadComponent, {
      width: '1000px',
      //data: {name: this.name, animal: this.animal}
    });

    dialogRef.afterClosed().subscribe(result => {
// I want to get data from upload response here! **res**
    });
  }

这个组件触发 PicuploadComponent 我上传图片并接收一些数据的响应

onUpload() {
  const fd = new FormData(); 
  fd.append('image', this.selectedFile);
  this.service.uploadImage(fd).subscribe(
    (res:any)=>
    {
      console.log(res) ;
      this.dialogRef.close();
    },    
  );
}

【问题讨论】:

标签: angular typescript components implementation data-transfer


【解决方案1】:

PicuploadComponent 中试试这个:

this.dialogRef.close(res);

访问组件A中的资源:

dialogRef.afterClosed().subscribe(result => {
  console.log(result)
});

【讨论】:

    【解决方案2】:

    当你关闭对话框时。

    this.dialogRef.close({data:'data'});
    

    当对话框完全关闭时。

    let dialogRef = this.dialog.open(PicuploadComponent, dialogConfig).afterClosed()
      .subscribe(response => {
        console.log(response);
      });
    

    如果您有任何疑问,请告诉我。谢谢。

    【讨论】:

      【解决方案3】:

      您应该将uploadImage 调用的结果传递给this.dialogRef.close() 方法。请参阅他们文档中的示例,https://material.angular.io/components/dialog/overview

      【讨论】:

        猜你喜欢
        • 2019-08-03
        • 1970-01-01
        • 2019-12-26
        • 1970-01-01
        • 1970-01-01
        • 2018-09-20
        • 2017-02-15
        • 1970-01-01
        相关资源
        最近更新 更多