【问题标题】:Capturing the close using promises使用 Promise 捕获收盘价
【发布时间】:2015-07-24 11:39:37
【问题描述】:

我有一个问题,因为我需要在 ui-bootstrap 模态关闭(并已完成动画)时执行一个方法。我不知道如何正确地写承诺。到目前为止我有这个(在 TypeScript 中):

public open(attrs, opts) {
    var scope = this.rootScope.$new();
    angular.extend(scope, attrs);

    opts = angular.extend(opts || {}, {
        backdrop: false,
        scope: scope,
        templateUrl: 'splash/content.html',
        windowTemplateUrl: 'splash/index.html'
    });

    this.modalInstance = this.modal.open(opts);

    this.modalInstance.result.then(
        function() {
            console.log("1");
        },
        function() {
            console.log("2");
        },
        function() {
            console.log("3");
        });
}

public close() {
    if (this.modalInstance != null && this.modalInstance != undefined) {
        this.modalInstance.close();
    }
}

我尝试将 .result 附加到 close 方法,但显然没有退出:似乎只有 open 方法返回一个承诺。谁能帮我吗?控制台中只显示“1”,我认为这是开放解析。

【问题讨论】:

  • .result 是通过.close.dismiss 关闭模式时的解析。从文档中 - “当模式关闭时解决的承诺并在模式被关闭时被拒绝”

标签: javascript angularjs twitter-bootstrap promise angular-ui-bootstrap


【解决方案1】:

angular-ui-bootstrap 模态结果属性返回一个你已经发现的承诺。如果模态框关闭(已确认/是),则承诺已解决,但如果模态框被关闭(在模态框外单击/取消),则承诺将被拒绝。

如果你想在模式关闭后调用多个函数,你可以用额外的 .then() 调用链接它们。

您可以使用 .catch() 查看模态是否被取消/关闭。

this.modalInstance.result.then(function() {
  console.log(1);
}).then(function() {
  console.log(2);
}).then(function() {
  console.log(3);
}).catch(function(err) {
  console.log('the modal was dismissed so the promise chain was rejected');
});

【讨论】:

    猜你喜欢
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多