Angular 使用浏览器后退时 $location 不更新

Angular $location not updating when browser back is used

有人 运行 经历过这种行为吗?关于它的提及不多,其他帖子性质不同。这似乎与有关浏览器行为的文档不一致。

Synchronizes the URL with the browser when the user

  • Changes the address bar.
  • Clicks the back or forward button (or clicks a History link).
  • Clicks on a link.

https://docs.angularjs.org/api/ng/service/$location#!

会发生什么:

这是现场帮助调试的观察者:

// listen for the event in the relevant $scope
$rootScope.$on('locationChange', function (event, data) {
    console.log(data);
});

//Call locationChange watch at anytime the page is loaded 
$scope.$emit('locationChange', $location.$$path);

这是路由:

$routeProvider
    .when('/guide', {
        templateUrl: 'views/guide.html',
        controller: 'GuideController',
        controllerAs: 'guide'
    })
    .when('/', {
        templateUrl: 'views/about.html',
        controller: 'AboutCtrl',
        controllerAs: 'about'
    })
    .otherwise({
        redirectTo: '/'
    });

我是如何解决这个问题的:

var pop = 0;

window.onpopstate = function(event) {
    if (document.location.pathname === '/' && pop > 1) {
        pop = 0;
        document.location = 'http://localhost:9000/';
    }
    pop++;
};