How to trigger the event cancel when I click outside the modal or ESC constraint?

1

Problem with output from modal $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 outside the modal, it closes the modal, but does not trigger the cancel event. I know that if I change backdrop: 'static' and keyboard: false, it locks the modal closure, but this way it does not look cool, I just want it to execute the cancel method inside the modal control when the person clicks out of modal. / p>

$scope.cancel();

How to make it shoot?

    
asked by anonymous 03.11.2017 / 16:14

1 answer

1

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

Your code looks 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 the controller of your modal would have some code similar to this:

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

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

More information here .

    
03.11.2017 / 18:46