簡體   English   中英

使用ui-router禁用AngularJS中的后退按鈕

[英]Disabling back button in AngularJS with ui-router

我想為我的應用程序禁用后退按鈕(我知道這是愚蠢的,糟糕的用戶體驗,與世界隔絕,甚至是犯罪!),但我的客戶希望禁用后退按鈕:)。 無論如何,我為禁用后退按鈕做了以下操作。

我在window.onpopstate事件處理程序中設置一個標志,如下所示:

window.onpopstate = function() { window.backClicked = true; }

在我的主控制器中設置以下內容:

$rootScope.$on('$locationChangeStart', function (event, next, current) {
                if (window.backButton) {
                    event.preventDefault();
                    window.backButton = false;
                }
            });

我想知道我的方法不好嗎?

我使用了其他類似的流行解決方案:

 window.onpopstate = function(event) {
    history.go(1); // or window.forward()
  }; 

但是它在chrome中不起作用,它第一次無法工作並且在狀態之間循環,並且window.forward()不會重新加載狀態,更准確地說:如果我有三個名為A,B,C的狀態分別使用/stateA/stateB/stateC URL,並且用戶處於狀態B,他突然按下返回按鈕,將url更改為/stateA並加載了狀態A,將url更改為/stateB但未加載狀態B且url為/stateB但加載的狀態為A。

對后一種方法有什么解決辦法或想法嗎?

順帶一提,我在這個問題上苦苦掙扎超過三天。

  $rootScope.$on('$locationChangeStart', function (event, next, current) { if (window.backButton) { event.preventDefault(); window.history.forward();// keep redirecting to the same page } }); 

event.preventDefault()將防止向后移動,並且window.history.forward()將保持相同的頁面,讓我們試試吧!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM