[英]AngularJS $location service and “Back” button
For example, current URL is " http://localhost/#/mail ". 例如,当前URL是“ http:// localhost /#/ mail ”。 I do some actions, and I want to go to the next url: 我做了一些动作,我想转到下一个网址:
$location.path("mail/inbox/3")
It works good. 效果很好。 I'm in another controller with another URL. 我在另一个具有另一个URL的控制器中。 But when I click by "Back" browser button I hope to be returned to "mail" route, but instead of it I'm gone to "mail/inbox" route. 但是,当我单击“后退”浏览器按钮时,希望返回到“邮件”路由,但我改为使用“邮件/收件箱”路由。 Why? 为什么? How can I fix it? 我该如何解决? Because it's incorrect for me. 因为对我来说这是不正确的。 Thanks. 谢谢。
When you fire up this baby: 当您解雇这个孩子时:
$location.path("mail/inbox/3")
Do remember that it traversed through when('mail')
and when('mail/inbox')
to reach when('/mail/inbox/:number')
. 请记住 ,它遍历了when('mail')
和when('mail/inbox')
到达when('/mail/inbox/:number')
。 The route went from mail -> inbox -> #3
. 路由从mail -> inbox -> #3
出发。 This is why when you hit back it goes to mail/inbox
. 这就是为什么当您回击时它将转到mail/inbox
。
This is a messy situation to fix. 这是一个混乱的情况。 ng-router
or ui-router
are good when thing are simple, but as application grows, bugs like your are unavoidable. ng-router
或ui-router
在简单的情况下会很好,但是随着应用程序的增长,像您这样的错误是不可避免的。
In my projects I disable the html5Mode
with $locationProvider.html5Mode(false);
在我的项目中,我使用$locationProvider.html5Mode(false);
禁用了html5Mode
$locationProvider.html5Mode(false);
, essentially disabling the back button. ,实际上是禁用后退按钮。 And add custom back buttons inside the App for user to navigate back. 并在应用程序内添加自定义后退按钮,以供用户向后导航。
This solution helps with: 该解决方案有助于:
I hope this helps. 我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.