简体   繁体   中英

AngularJS $location service and “Back” button

For example, current URL is " 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. 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') . The route went from mail -> inbox -> #3 . This is why when you hit back it goes to 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.

In my projects I disable the html5Mode with $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:

  • User cant use back button at all, so he can't reach an unwanted state
  • Custom back buttons can be configured or hidden at times. Event code to preform differently on same view, based on current app state.
  • Hide the app internal URL from users to avoid the app being exploited for data the user is not supposed to see.

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM