2016-01-14 35 views
1

有没有人跑过这种行为?关于它并没有太多提及,其他职位本质上是不同的。这似乎与关于浏览器行为的文档不一致。

与同步,当用户

  • 更改地址栏中的浏览器的URL。
  • 点击后退或前进按钮(或点击历史链接)。
  • 点击链接。

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: '/' 
    }); 
+0

你是什么意思当你说“随时加载页面”?该页面只加载一次,然后用路由器根据路径创建和销毁控制器。你能提供你发出事件的代码吗? –

+0

正确,页面只加载一次,无论您是在根区还是小孩路线/视图。 $ location。$$ path在所有应用程序更改触发的函数中被省略,但在您点击时单击浏览器并不会更新。 Angular何时以及如何跟踪后退/前进按钮的点击? – vars

回答

1

我如何解决这样的:

var pop = 0; 

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