javascript - 单击外部/esc时,Angularjs Bootstrap模式关闭调用

标签 javascript angularjs angular-ui angular-ui-bootstrap

我正在使用Angular-ui/bootstrap我的项目中的模态。

这是我的模式:

$scope.toggleModal = function () {
    $scope.theModal = $modal.open({
        animation: true,
        templateUrl: 'pages/templates/modal.html',
        size: "sm",
        scope: $scope
    });
}

可以通过单击 ESC 按钮或单击模态区域外部来关闭模态。发生这种情况时有办法运行函数吗?我不太确定如何捕捉这种结束语。

我知道我可以通过使用 ng-click="closeModal()" 来手动关闭模式,如下所示:

$scope.closeModal = function () {
    $scope.theModal.dismiss('cancel');
};

最佳答案

是的,你可以。它会导致解雇事件,并且在这种情况下 promise 将被拒绝。另请注意,$modal.open() 方法返回一个对象,该对象具有作为 promise 的 result 属性。

有了 promise ,你就可以...

//This will run when modal dismisses itself when clicked outside or
//when you explicitly dismiss the modal using .dismiss function.
$scope.theModal.result.catch(function(){
    //Do stuff with respect to dismissal
});

//Runs when modal is closed without being dismissed, i.e when you close it
//via $scope.theModal.close(...);
$scope.theModal.result.then(function(datapassedinwhileclosing){
    //Do stuff with respect to closure
});

作为快捷方式,您可以编写:

 $scope.theModal.result.then(doClosureFn, doDismissFn);

See ref

The open method returns a modal instance, an object with the following properties:

  • close(result) - a method that can be used to close a modal, passing a result
  • dismiss(reason) - a method that can be used to dismiss a modal, passing a reason
  • result - a promise that is resolved when a modal is closed and rejected when a modal is dismissed
  • opened - a promise that is resolved when a modal gets opened after downloading content's template and resolving all variables 'rendered' - a promise that is resolved when a modal is rendered.

关于javascript - 单击外部/esc时,Angularjs Bootstrap模式关闭调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30356844/

相关文章:

angularjs - 使用 angular-google-maps 刷新 map 和标记

angularjs - 适用于 Angular 的 UI 套件,可与 IE8 配合使用

表单输入和选择的 javascript index()

javascript - 在 ExtJS 中检索运行时组件维度

javascript - 如何使用 angularJS 从页面注销

javascript - ng-click 不会在已编译的模板内触发

angularjs - Angular-UI Bootstrap typeahead ngmodel 在放置在选项卡内时无法按预期工作

java - 在 Java/JavaScript 中解析 ISO-8601 持续时间

javascript - CoffeeScript 和 OOP - 原型(prototype)方法

angularjs - angularjs 中的工具提示,带有 "uib-tooltip-html"