$uibModal:从方法中获取实例

$uibModal: get instance from method

本文关键字:获取 实例 方法 uibModal      更新时间:2023-09-26

我通过方法使用情态动词,如:

this.showModal = function () {
  return $uibModal.open({...});
}

然后在某个方法中调用这个函数:

this.showModal().result.then(function (someResult) {...});

但是我如何使用解散,使用方法调用?

因为我使用它没有方法,我可以关闭我的模态,所以:

$uibModal.open({...}).result.then(function (someResult) {
  this.$dismiss();
})

但是我没有线索,如何使用解散,当我使用方法承诺…

也许有人有主意?

open方法返回一个带有open, closed,dismiss ,close,rendered ,returned方法的模态实例。

在你的情况下,它是this.showModal是模态实例。

所以,你可以像这样调用close方法。

var self = this;
self.showModal = function () {
  return $uibModal.open({...});
}
//close the modal
self.showModal.close();

你可以将模态存储在对象中,或者在作用域中,或者将其移动到服务中,然后在模态对象上调用解散

var modalInstance;
this.showModal = function () {
  modalInstance = $uibModal.open({...});
  return modalInstance;
}

和使用promise

this.showModal().result.then(function (someResult) {
    modalInstance.dismiss('cancel');
});

我通常做的是创建一个模态实例:

$scope.doSomething = function(){
     var modalOptions = {...};
     openModal(modalOptions);
}
function openModal(modalOptions) {
            $scope.myModalInstance = $uibModal.open(modalOptions);
            $scope.myModalInstance.result.then(function () {
                //success callback
            }, function () {
                //error callback;
            });
        }

之后如果我想调用close或解散,只需调用$scope.myModalInstance.close()$scope.myModalInstance.dismiss('cancel')