【问题标题】:How to pass result from angular 8 material dialog to other component?如何将角度 8 材质对话框的结果传递给其他组件?
【发布时间】:2021-06-28 08:46:47
【问题描述】:

我在 Component1 中有一个对话框 Component2。当我在 Component2 中有一个 result = true 时,我想在 component1 中调用一个函数。

所以,我在Component1 中有这个代码:

  searchbyenumber() {
    const dialogRef = this.dialog.open(Component2, {
    });
    dialogRef.afterClosed().subscribe(result => {
      if (result) {
        this.LoadTrainingSessionPartecipants();
      }
    });
  }

在 Component2 我有这个代码:

  public dialogRef: MatDialogRef<Component2>

      confirmYes() {
        let Enrollment = new EnrollmentModel();
        Enrollment.PersonId = PersonId.toString();
        this.dataservice.SetEnrollment(Enrollment).then(
          result => {
            if (result) {
              console.log('result11', result)
            } else {
            }
          }
        );
      }

所以当我从这个函数 confirmYes() 得到结果为真时,我想执行 searchbyenumber() 函数 this.LoadTrainingSessionPartecipants() 我在 Component1 中的函数

请问您能告诉我如何称呼它吗?

【问题讨论】:

    标签: angular dialog


    【解决方案1】:

    你应该在你的对话框组件(Component2)中注入MatDialogRef,然后当你得到结果并可以关闭对话框时,只需调用它的方法close()将结果作为参数传递;这将触发afterClosed,您可以在其中访问 Component1 中的数据,您的那部分代码似乎是正确的。

    在 Component2 中你应该这样做:

     constructor(private dialogRef: MatDialogRef<Component2> // <-- INJECTION
                 ... other injections
                 ) { }
    
     confirmYes() {
        let Enrollment = new EnrollmentModel();
        Enrollment.PersonId = PersonId.toString();
        this.dataservice.SetEnrollment(Enrollment).then(
          result => {
            if (result) {
              console.log('result11', result);
              this.dialogRef.close(result); // <-- HERE
            } else {
            }
          }
        );
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-14
      • 2017-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-19
      相关资源
      最近更新 更多