Returning data from angular $uibModal backdrop click

送分小仙女□ 提交于 2019-12-12 09:47:18

问题


I'm trying to pass back a value from a uibModal. I can define the return if the user clicks the modal's close button

$scope.close = function () {
    $modalInstance.close($scope.editMade);
};

But this doesn't work if the user click the backdrop area.

How can I define a return value for that particular event?


回答1:


When you click on the backdrop outside, it does a dismiss internally.

Try using this inside modal:

$modalInstance.dismiss($scope.editMade);

And use this to handle data:

 instance.result.then(function(){
  //Get triggers when modal is closed
 }, function(){
  //gets triggers when modal is dismissed. You can basically handle data here
 });

Check this working plunker. It uses the dismiss as I mentioned

http://embed.plnkr.co/YdWmqYPFBNbv4vhQZc4t/

Passing custom data to your parent controller when modal is dismissed:

Code in Modal Controller:

  $scope.$on("modal.closing",function(){
       $rootScope.$broadcast("modalClosing",$scope.editMade);
  });

Then in your parent controller:

 $scope.$on("modalClosing",function(event,value){
    console.log(value); //value should be $scope.editMade
  });

You can learn more by reading the documentation here: https://github.com/angular-ui/bootstrap/tree/master/src/modal/docs




回答2:


In the modal controller, you can do something like this:

$scope.$on('modal.closing', function(event, reason, closed) {
                if (reason == "backdrop click" || reason == "escape key press")
                {
                    event.preventDefault();
                    $uibModalInstance.close({"data": somedata});
                }                
            });

This way you'll always get the success callback on modalInstance

modalInstance.result.then(function(response) {
// here response will be whatever is passed. in this sample {"data": somedata}
});


来源:https://stackoverflow.com/questions/38684701/returning-data-from-angular-uibmodal-backdrop-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!