检测历史背采用了棱角分明

检测历史背采用了棱角分明

本文介绍了检测历史背采用了棱角分明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能检测到用户使用,通过在浏览器历史后退按钮进入一个页面? preferably我想用angular.js检测到这个动作。

Is it possible to detect that a user entered a page through using the history back button in his browser? Preferably I want to detect this action using angular.js.

我不想使用角路由。如果用户提交表单还应该工作,成功后提交给服务器和重新定向,也应该在用户使用浏览器的后退按钮返回到形式是可能的。

I do not want to use angular routing. It should also work if a user submits a form and after a successful submit to the server and a re-direct, it should also be possible if the user goes back to the form using the back button of the browser.

推荐答案

下面是角度的解决方案。

Here is a solution with Angular.

myApp.run(function($rootScope, $route, $location){
   //Bind the `$locationChangeSuccess` event on the rootScope, so that we dont need to
   //bind in induvidual controllers.

   $rootScope.$on('$locationChangeSuccess', function() {
        $rootScope.actualLocation = $location.path();
    });

   $rootScope.$watch(function () {return $location.path()}, function (newLocation, oldLocation) {
        if($rootScope.actualLocation === newLocation) {
            alert('Why did you use history back?');
        }
    });
});

我使用的是运行程序段的kickstart这一点。首先,我存储在$ rootScope.actualLocation的实际位置,然后我听$ locationChangeSuccess,当它发生了,我更新了新的价值actualLocation。

I am using a run block to kickstart this. First I store the actual location in $rootScope.actualLocation, then I listen to $locationChangeSuccess and when it happens I update actualLocation with the new value.

在$ rootScope我看在位置路径,如果改变的新位置等于previousLocation是因为$ locationChangeSuccess没有被解雇,这意味着用户已经使用的历史了。

In the $rootScope I watch for changes in the location path and if the new location is equal to previousLocation is because the $locationChangeSuccess was not fired, meaning the user has used the history back.

下面是。

这篇关于检测历史背采用了棱角分明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:26