afterClosed()的角材料对话框

时间:2018-08-06 09:31:30

标签: angular promise return angular-material

我想使用Angular Material对话框创建一个yes / no函数,该函数返回true或false取决于是否按下yes或no。在是/否对话框中,有以下按钮:

<button [mat-dialog-close]="true">Yes</button>
<button [mat-dialog-close]="false">No</button>

在我的dialogservice中,我通过以下方式调用此是/否对话框:

Yes_No_Prompt(pMessageLines: string[]) {
    const yesNoDialog = this.matdialog.open(DlgYesNoComponent, {
        data: {
            MessageLines: pMessageLines
        }
    });

    yesNoDialog.afterClosed().subscribe(
        (result: boolean) => {
            return result;
        }
    );
}

但是如何使此函数等待行return result?因为我现在想通过执行以下操作来使用此功能:

if (this.dialogservice.Yes_No_Prompt()) {
    // something is true (pressed yes)
} else {
    // something if false (pressed no)
}

我需要等待return result行,但我不知道如何。可能有诺言吗?

1 个答案:

答案 0 :(得分:3)

执行Observable时,您已经在使用subscribe。像这样从您的Yes_No_Prompt返回Observable:

Yes_No_Prompt(pMessageLines: string[]): Observable<boolean> {
  const yesNoDialog = this.matdialog.open(DlgYesNoComponent, {
    data: {
        MessageLines: pMessageLines
    }
  });

  return yesNoDialog.afterClosed();
}

然后

this.dialogservice.Yes_No_Prompt().subscribe(result => {
   if(result) {
     // something is true (pressed yes)
   } else {
     // something if false (pressed no)
   } 
});