本文介绍了AngularJS 重定向而不推送历史状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 AngularJS 应用程序,该应用程序具有支持旧版 url 格式所必需的所有路由(例如,.when('/:slug', {...))来自应用程序的先前(非角度)版本.响应 catch all 的控制器尝试拉出相关对象,如果没有找到,则使用 $location.path 方法重定向到 404 页面.这有助于让用户进入 404 页面,但是当用户在他们的浏览器中回击时,它会将他们带回到最初强迫他们进入 404 页面的页面,他们最终无法摆脱循环.

I'm working on an AngularJS app that has a catch all route (eg, .when('/:slug', {...)) which is necessary to support legacy url formats from a previous (non-angular) version of the app. The controller that responds to the catch all tries pulling a related object, and, if not found, redirects to a 404 page using the $location.path method. This works at getting the user to the 404 page, but when the user hits back in their browser it takes them back to the page that forced them to the 404 page in the first place and they end up being unable to escape the cycle.

我的问题是是否有 1) 处理这种情况的更好模式,或者 2) 是否有一种方法可以重新路由用户而不强制浏览器中的历史推送状态?

My question is if there is 1) a better pattern for handling this situation, or 2) if there is a way to reroute the user that doesn't force a history push state in the browser?

推荐答案

可以在不添加历史状态的情况下更改 url,找到 此处 在替换方法"下.这实际上与调用 HTML5 的 history.replaceState() 相同.

You can change the url without adding to the history state, found here under "Replace Method". This is effectively the same as calling HTML5's history.replaceState().

$location.path('/someNewPath').replace();

我还没有发现可以在不更改 url 的情况下更改视图.我发现更改视图的唯一方法是更改​​位置路径.

I haven't found that it's possible to change the view without changing the url. The only method to change the view, that I've found, is to change the location path.

这篇关于AngularJS 重定向而不推送历史状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 01:34
查看更多