2016-07-31 31 views
1

我試圖從uibModal傳回一個值。如果用戶點擊模式的關閉按鈕

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

我可以定義返回但是如果用戶點擊背景區域,這並不工作。

如何定義特定事件的返回值?

回答

3

當你點擊外面的背景時,它會在內部解除。

嘗試使用這裏面的情態:

$modalInstance.dismiss($scope.editMade); 

,並以此來處理數據:

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

檢查這個工作plunker。它使用解僱正如我所提到

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

傳遞自定義數據到父控制器時,模態辭退:

代碼在模態控制器:

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

然後在你的父控制器:

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

你可以在這裏閱讀文檔瞭解更多信息: https://github.com/angular-ui/bootstrap/tree/master/src/modal/docs

+0

我已將解僱添加到我的模態,但我仍然無法將該變量傳回給父控制器 – mrb398

+0

@ user1691808:檢查正在工作的解僱者 –

+0

爲了說明我試圖完成的任務,plunker行「$ log.info('Modal駁回:'+ new Date());」是我試圖返回「selectedItem」的地方 – mrb398

0

在模態控制器,你可以做這樣的事情:

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

這樣,你將永遠得到modalInstance

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