You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular Material 2 Beta8中MdDialog.afterAllClosed返回undefined问题求助

Angular Material 2 Beta8: MdDialog.afterAllClosed returns undefined instead of dialog close result

Hey there, let's break this down for you — this isn't actually a bug in Beta8, it's just a mix-up in which observable you're using to capture the dialog's return value.

The Core Issue

MdDialog.afterAllClosed is designed to fire only when every open dialog has been closed — it doesn't carry any specific dialog's result (which is why you're seeing undefined). The observable you actually need to get the return value from a single dialog is MatDialogRef.afterClosed().

Fixed Code Examples

Parent Component (corrected to use the right observable)

Instead of subscribing to the global afterAllClosed, you should subscribe to the afterClosed() stream returned directly by the dialog instance you opened:

public openDeleteDialog(): void {
  const dialogRef = this.dialog.open(DeleteCompanyDialogComponent, {
    width: environment.dialogSmallWidth,
    height: environment.dialogSmallHeight,
    data: {company: this.company, deleted: false}
  });

  // Subscribe to THIS specific dialog's close event to get your result
  dialogRef.afterClosed()
    .subscribe(result => {
      console.log(result); // This will now log 'LOH' as expected
      console.log('delete closed');
    });
}

Dialog Component (your existing code is already correct!)

public closeDialog(): void {
  this.dialogRef.close('LOH');
}

onNoClick(): void {
  this.dialogRef.close('LOH');
}

Why This Behaves This Way in Beta8

In Angular Material 2 Beta8, afterAllClosed was purely a notification that all dialogs are no longer open — it was never meant to pass through individual dialog results. The afterClosed() method on the dialog reference is the intended, documented way to capture the value passed to close().

If you ever do need to react when all dialogs are closed (regardless of individual results), afterAllClosed is still valid — but it won't give you specific return values from any single dialog.

内容的提问来源于stack exchange,提问作者Deniss M.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:34:08