【问题标题】:How to detect closing a modal window in $modal? angular-ui如何检测在 $modal 中关闭模式窗口?角度-ui
【发布时间】:2014-10-10 12:38:36
【问题描述】:

我在angular-ui中使用$modal创建一个模态窗口,她是创建模型的代码。

this.show = function (customModalDefaults, customModalOptions, extraScopeVar) {
    //Create temp objects to work with since we're in a singleton service
    var tempModalDefaults = {};
    var tempModalOptions = {};

    //Map angular-ui modal custom defaults to modal defaults defined in service
    angular.extend(tempModalDefaults, modalDefaults, customModalDefaults);

    //Map modal.html $scope custom properties to defaults defined in service
    angular.extend(tempModalOptions, modalOptions, customModalOptions);

    if (!tempModalDefaults.controller) {
        tempModalDefaults.controller = function ($scope, $modalInstance) {
            $scope.modalOptions = tempModalOptions;
            $scope.extraScopeVar = extraScopeVar;
            $scope.modalOptions.ok = function (result) {
                $modalInstance.close(result);
            };
            $scope.modalOptions.close = function (result) {
                $modalInstance.dismiss('cancel');
            };
        }
    }

    return $modal.open(tempModalDefaults).result;
};

到目前为止,一切正常。当我在模式窗口上点击取消或确定时,模型会关闭和关闭。问题是,当我在模态窗口外按一下点击时,它会消失我想要的东西,但我也知道在这种情况下运行哪种方法来覆盖它以实现自定义行为,就像我正在使用 OK 和 Close 所做的那样

【问题讨论】:

  • 关闭方法,这是一种承诺拒绝

标签: javascript jquery angularjs angular-ui


【解决方案1】:
$modal.open(tempModalDefaults).result

是一个承诺。当背景选项设置为“真”时,承诺将被拒绝。您可以使用 then 方法或 catch 方法(快捷方式)将拒绝处理程序附加到该承诺:

catch(errorCallback) – promise.then(null, errorCallback) 的简写

return $modal.open(tempModalDefaults).result.catch(function (reason) {
    // your code to handle rejection of the promise.
    if (reason === 'backdrop') {
        // do something
    }
});

【讨论】:

    猜你喜欢
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    • 2023-02-03
    • 2020-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    相关资源
    最近更新 更多