首页 > 解决方案 > TypeScript 模态对话框承诺响应

问题描述

我的应用程序中有一个显示引导对话框的服务。目前代码非常简单:

async showConfirmation(message: string): Promise<boolean> {

    this.message(message);
    this.isPopupOpen(true);

    return false;
}

目前,对话框 HTML 如下所示:

<div role="dialog" aria-hidden="true" class="modal fade" data-bind="modal: { show: isPopupOpen }">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" title="Close" aria-label="Close">&times;</button>
        <h4 class="modal-title">My Modal</h4>
      </div>

      <div class="modal-body" data-bind="text: message"></div>

      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal" data-bind="click: close">Cancel</button>
        <button type="button" class="btn btn-primary" data-dismiss="modal" data-bind="click: close">OK</button>
      </div>
    </div>
  </div>
</div>

我的视图模型中的关闭处理程序也很简单,像这样:

close(data, event) {

    //console.debug(data);
    console.debug(event.target);

    this.isPopupOpen(false);
}

目前该showConfirmation函数立即返回一个硬编码false,而我想等待用户响应并将其作为showConfirmation.

我不完全确定实现这一目标的最佳方法。我怎样才能让它等待用户以返回适当的响应?

标签: javascripttypescriptpromise

解决方案


推荐阅读