gpt4 book ai didi

javascript - 使用浏览器后,Angular $location 不会更新

转载 作者:行者123 更新时间:2023-11-27 23:27:58 26 4
gpt4 key购买 nike

有人遇到过这种行为吗?对此没有太多提及,其他帖子性质不同。这似乎与有关浏览器行为的文档不一致。

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++;
};

关于javascript - 使用浏览器后,Angular $location 不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34800757/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com