将订阅结果发送回 angular material 对话框

Send subscription results back to angular material dialog

因此,当我从对话框中触发事件发射器时: ...

onAdd = new EventEmitter();

onButtonClick() {
  this.onAdd.emit();
}

我听家长说的:

let dialogRef = this.dialog.open(Component);
const sub = dialogRef.componentInstance.onAdd.subscribe(() => {
  myservice.subscribe(results => /*send back to dialog*/)
});

如何将 "results" 发送回同一对话框?

编辑:我还需要在 "results" 准备就绪时在对话框中触发函数调用。

您可以通过这种方式将值传回对话框:

 const sub = dialogRef.componentInstance.onAdd.subscribe(() => {
  myservice.subscribe(results => {
    // passes results to your dialog. you need to create a property 'results' in you dialog class though.
    dialogRef.componentInstance.results = results; 
    dialogRef.componentInstance.someFunction(); // function you want to trigger after results is ready
  })
});

因为 dialogRef.componentInstance 为您提供了对话框的完整实例 class,您可以在对话框 class 本身中执行操作。