2015-12-31 36 views
2

我正在使用UI Bootstrap AngularUI,我需要在任何模式關閉時調用函數。 我知道我可以調用一個函數,當一個特定的模式實例關閉這樣的:AngularUI Bootstrap模式關閉事件對於任何insta

modalInstance.result.finally(function(){ 
    console.log('Modal closed'); 
}); 

在jQuery中我會爲任何情況下做這樣的事情:

$(document).on('hidden.bs.modal', function() { 
    console.log('Modal closed'); 
}); 

但是,如何實現與AngularJS?

回答

3

你可以做這樣的事情:

myApp.controller('ModalCtrl', ['$scope', '$modal', function($scope, $modal) { 
    $scope.open = function() { 
     var modalInstance = $modal.open({ 
      templateUrl: 'modal.html', 
      controller: 'ModalInstanceCtrl', 
      resolve: { 
       params: function() { 
        return { 
         key: 'value', 
         key2: 'value2' 
        }; 
       } 
      } 
     }); 
     modalInstance.result.then(
      function(result) { 
       console.log('called $modalInstance.close()'); 
       alert(result); 
      }, 
      function(result) { 
       console.log('called $modalInstance.dismiss()'); 
       alert(result); 
      } 
     ); 
    }; 
}]) 

從你controller東西你能。

myApp.controller('ModalInstanceCtrl', ['$scope', '$modalInstance', 'params', function ($scope, $modalInstance, params) { 
    console.log(params); 

    $scope.ok = function() { 
     $modalInstance.close('this is result for close'); 
    }; 

    $scope.cancel = function() { 
     $modalInstance.dismiss('this is result for dismiss'); 
    }; 
}]); 

發現一個整潔的小提琴在此: http://jsfiddle.net/wjohtz0y/

+0

嗨Thalaivar,你能回答這個問題https://stackoverflow.com/questions/43583136/bootstrap-modal-popup-open-and-close - 使用 - angularjs - 不工作 – Vinoth