2015-10-06 45 views
0

我確實有一個$modalInstance。我可以接收接近事件通知與承諾的幫助:如何防止Angular-UI bootstrap模式關閉?

$modalInstance.result.finally(function() { 
    // code here 
}); 

但我不知道如何防止關閉,如果用戶錯誤地關閉模式。我想詢問用戶是否真的想關閉模型並關閉模型。仍然我不想啓用backdrop: 'static'

$modal.open({ 
    ... // other options 
    backdrop : 'static' 
}); 

謝謝。

+0

當你關閉模式就像一個事件觸發,在我看來,你可以以某種方式工作,我認爲這個問題可以幫助你:http://stackoverflow.com/questions/30356844/angularjs-bootstrap- modal-closing-call-when-clicking-outside-esc – klskl

+0

我不認爲有必要使用boostrap的模態。您可以簡單地創建一個z-index大於零的div/form。並根據需要使用ng-hide/ng-show使隱藏/可見。 – SaiGiridhar

+0

@SaiGiridhar好吧,我必須在項目中使用它,這是一個要求。最後一次我做模態它只是VanillaJS :) – Georgy

回答

2

我做了一些更多的研究,我發現another question與此類似,這是對這個問題找到了答案(隨意+1他,而不是我)

$scope.$on('modal.closing', function(event, reason, closed) { 
    var r = prompt("Are you sure you wanna close the modal? (Enter 'YES' to close)"); 

    if (r !== 'YES') { 
     event.preventDefault(); 
    } 
}); 

將這個內部模態控制器。

這不完全是您搜索的內容,如果您嘗試關閉模式,可以通過關閉(單擊外部模式),取消或確定按鈕來提示您。您可以嘗試並修改它以滿足您的需求。

更新:新增簡單if else檢查,看看到底點擊了哪個,如果點擊背景,然後提示用戶:

$scope.$on('modal.closing', function(event, reason, closed) { 

    if (reason == 'ok' || reason == 'cancel'){ 
     console.log('closed'); 
    } else { 
     // this is if 'reason' == 'backdrop click' 
     var r = prompt("Are you sure you wanna close the modal? (Enter 'YES' to close)"); 

     if (r !== 'YES') { 
      event.preventDefault(); 
     } 
    } 
}); 

你認爲這個解決方案是足夠了嗎?

+0

Upvote鏈接到另一個問題。 –