2016-11-09 63 views
0

如果滿足特定條件,我需要用戶確認離開頁面。問題是位置變化不等待對話框來獲取用戶答案。不等待對話框內用戶回答的事件

這裏是我的代碼:

角模塊1:

... 
function confirmLeavePage(e, newUrl) { 
     if(form.cod.value) { 
      customDialog.confirmDialog('Title','Leave?').then(
      function(){ 
       console.log('go to selected page'); 
      },function(){ 
       e.preventDefault(); 
      }); 
     } 
    } 

    $scope.$on("$locationChangeStart", confirmLeavePage); 
... 

角模塊2:

angular.module('dialog').service('customDialog', function($mdDialog, $q, $location) { 

    this.confirmDialog = function(title, content){ 
     var deferred = $q.defer(); 
     $mdDialog.show($mdDialog.confirm({ 
      templateUrl:'confirmDialog.html', 
      title : title, 
      textContent : content, 
      ok : 'Confirm', 
      cancel: 'Cancel' 
     })).then(function() { 
      console.log('confirmed'); 
      deferred.resolve(); 
     }, function() { 
      console.log('abort'); 
      deferred.reject(); 
     }); 
     return deferred.promise; 
    } 


}); 

任何想法?

回答

0

試試這個

function confirmLeavePage(e, newUrl) { 
     if(form.cod.value) { 
      customDialog.confirmDialog('Title','Leave?').then(
      function(){ 
       console.log('go to selected page'); 
      }); 
     } 

     e.preventDefault(); 
     return; 
    } 
+0

我更換 '的console.log( '去選擇的頁面');' 與 '$ location.path(NEWURL)' 但它再次觸發對話,因爲它的抓住了 '$ scope。$ on(「$ locationChangeStart」,confirmLeavePage);' 任何想法? – Larsen