2017-10-06 6 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
私は、ページがリフレッシュされたりリロードしたかどうかを検出するための基準としてこれを使用することができことをお勧め

Check if page gets reloaded or refreshed in Javascript

また、ダイアログボックスの表示に役立つようにいくつかのコードを入力してください。

0

私は2つの解決策を得ました。これは、特定のページでダイアログを開くことです。

$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では機能しません。

2番目のものはhttps://gist.github.com/981746/3b6050052ffafef0b4df であり、自宅への確認後にリダイレクトされません。

0

あなたが角度にコーディングされているように試すことがあり、この

$window.onbeforeunload = function (e) { 
       return ""; 
      }; 
      $window.onload=function(){    
       window.location.href="gowhereever you want"; 
      } 
関連する問題