How do I trigger the Cancel event when I click out of the modal or Esc grip?

Asked

Viewed 230 times

1

Problem with modal output $uibModal:

var modalInstance = $uibModal.open({
                     animation: true,
                     templateUrl: 'detalhes_leitura.html',
                     controller: 'ModalController',
                     size: size,
                     backdrop:true,
                     keyboard:true,
                     resolve: {
                         dados: function () {
                             return $scope.result_items;
                         }
                     }});

When I click out of the modal, it closes the modal, but does not trigger the cancel event. I know that if you change backdrop:'Static' and Keyboard:false, it locks the modal closure, but this way it doesn’t look cool, I just want it to execute the Cancel method inside the modal control when the person clicks outside the modal.

$scope.cancel();

How to make it fire?

  • Adjust this Jsfiddle to your modal and see if it works: https://jsfiddle.net/ndfmcun5/

1 answer

1


In Angularui, the property result of the object returned by $uibModal.open is a Promise that solves when the modal is closed using the function $uibModalInstance.close and rejects when the modal is closed through $uibModalInstance.dismiss (also called when user clicks out of modal).

Your code would look like this:

var modalInstance = $uibModal.open({
  animation: true,
  templateUrl: 'detalhes_leitura.html',
  controller: 'ModalController',
  size: size,
  backdrop: true,
  keyboard: true,
  resolve: {
    dados: function() {
      return $scope.result_items
    }
  }
})

modalInstance.result
  .then(function(valor){
      // modal fechado usando $uibModalInstance.close
  })
  .catch(function(valor){
      // modal fechado usando $uibModalInstance.dismiss
      // ou clicando fora do modal
  })

In your modal controller would have some code like this:

app.controller('ModalController', function($scope, $uibModalInstance, dados){
   $scope.fecharModalSucesso = function(){
      $uibModalInstance.close()
   }

   $scope.fecharModalCancelar = function(){
      $uibModalInstance.dismiss()
   }
})

More information here.

  • But where would it call the close methodModalCancelar? I know the close methodThis can be put on the button of the modal template. but giving up doesn’t exist... I had to make a new div by grabbing the whole screen for it to click.

  • You can call the function fecharModalCancelar() on an cancel button, for example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.