在 Angular 应用中 location.reload 后后退按钮不起作用

Back button doesn't work after location.reload in Angular app

在我的应用程序中,我有一个创建 HTTP 承诺的按钮,并且在成功回调时刷新页面(使用 window.location.reload(true))。但是,这会将页面的新副本添加到浏览器历史记录中,因此后退按钮不起作用,因为它只是再次加载同一页面。知道我该如何解决这个问题吗?

如果可能的话,我正在尝试支持 ie8。

对您问题的评论指出您的方法不是最优的。不要完全重新加载一切,你应该有一个更新(已经加载的)视图的函数。

但是,如果您真的坚持 使用此行为,您可以改用 location.replace(),将当前 URL 传递给它。这实际上将重新加载页面,而不会在历史堆栈中添加重复项。

看到这个:https://docs.angularjs.org/guide/$location

There is a special replace method which can be used to tell the $location service that the next time the $location service is synced with the browser, the last history record should be replaced instead of creating a new one. This is useful when you want to implement redirection, which would otherwise break the back button (navigating back would retrigger the redirection). To change the current URL without creating a new browser history record you can call:

$location.path('/someNewPath');
$location.replace();
// or you can chain these as: $location.path('/someNewPath').replace();

注意:如果您正在使用 AngularJS,我建议您坚持使用框架提供给您的 $services,因为它们的行为与 JavaScript 对应的服务相同,除了它们还将用 $scope.$apply 包装起来,以防止您的 Angular 应用程序中出现任何不一致。