2017-10-06 43 views
4

如何在用戶嘗試刷新或重新加載頁面時打開對話框?如何打開模型或對話框?

我已經嘗試了很多鏈接,建議我扣除keypress/keydown/beforeload/beforeunload刷新,但這些沒有奏效。

這裏是,我試過了。與我的場景。

var App = angular.module('App', ['ngRoute']); 
     // configure our routes 
     App.config(function($routeProvider) { 
      $routeProvider 
       .when('/', { 
        templateUrl : 'home.html', 
        controller : 'mainController' 
       }) 
       .when('/about', { 
        templateUrl : 'about.html', 
        controller : 'aboutController' 
       }) 
       .when('/contact', { 
        templateUrl : 'contact.html', 
        controller : 'contactController' 
       }); 
     }); 

App.controller('mainController', function($scope) { 
     //but not here if the user refresh the page 
     $scope.message = 'MainCtrl!';      
    }); 

    App.controller('aboutController', function($scope) { 
     $scope.message = 'aboutController'; 
     // if this is the current controller and page being refresh by the user activity 
     // prompt the user to save before you reload... 
    }); 

    App.controller('contactController', function($scope) { 
     $scope.message = 'contactController'; 
    // if this is the current controller and page being refresh by the user activity 
     // prompt the user to save before you reload... 
    }); 

in index.html 
-------------------- 
<script> 
    //it is working . it is invoking in each page. But needs to invoke at certain controller means it should check 
    // it is current/exact page where user has to save data before reloading 

    jQuery(function() {   
    // to check and get page refreshed & reload 

    var myEvent = window.attachEvent || window.addEventListener; 
    var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; 

    myEvent(chkevent, function (e) { 
     var confirmationMessage = ' '; 
     (e || window.event).returnValue = confirmationMessage; 
     return confirmationMessage; 
    }); 
    if (myEvent) { 
     localStorage.clear(); 
     // redirect to 
     $state.go('gotohome');         
    } 
}); 
</script> 

任何人都可以給我任何功能的例子,可以在所有類型的瀏覽器?

對我來說非常重要,無論如何我都無法做到。

謝謝。

+0

你要說什麼,你嘗試作爲代碼,我們可以幫你的代碼。 –

+0

@artgb,我編輯並給出了我的場景。 – uday214125

回答

0

我得到了兩個解決方案之一是我的策略是打開某些頁面上的對話框。

$rootScope.refreshmodal= false; 
    $rootScope.refreshModal = function(){ 
    $rootScope.refreshmodal = $rootScope.refreshmodal ? false : true; 
} 



window.onload = function (e) {      
    if(e.returnValue){ 
     if(e.currentTarget.location.pathname == "/contact") { 
      //alert("ask to user ") 
      $rootScope.refreshmodal=true; // opening a UI modal 
     }else if(e.currentTarget.location.pathname == "/about"){ 
      $rootScope.refreshmodal=true; // opening a UI modal         
     }else { 
       alert(" donot ask "); 
      } 
    } 

這不適用於onbeforeload。

第二個是從這裏:https://gist.github.com/981746/3b6050052ffafef0b4df 它並沒有重定向後確認回家。

0

因爲你是在角編碼,你可以嘗試這個

$window.onbeforeunload = function (e) { 
       return ""; 
      }; 
      $window.onload=function(){    
       window.location.href="gowhereever you want"; 
      }