-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Behavior of $location.path() inconsistent when back and forward buttons are used #4989
Comments
Good catch. It looks like this issue has been around since 1.1.5 (possibly earlier). |
Well, I don't think that case is a bug. When you call $location.path inside of locationChangeStart, you are getting the current path. Let me use your plunk: Click on Test 1 and then click on Test 2:
Now, if you click on Back link, the $locationChangeStart will show Test 2 on location.path, because you're still in test2 page. $locationChangeStart will run before everything else, including the $browser.url that really change the URL on location.js and locationChangeSuccess is being executed after everythin so will show the new page. So, after click on back, you will see:
Location.path on locationChangeStart is showing test2, but the current page is: test1. So, now if click on forward, you will have:
Because your current page is test1 and you are going to test2. The code on locationChangeStart was not executed yet, so you will see test1 (current page) when call location.page. |
@phelipemaia Your premise, "When you call $location.path inside of locationChangeStart, you are getting the current path", is invalidated by your first example. Notice how during a link click the values of I'm not expressing any opinion about which is correct, just pointing out the inconsistency, which very much seems like a bug. |
Ok, let's take a look on location.path doc:
As you can see, it will return the current URL.
I will create a pull request and we will see what angular team will say about that.... |
I noticed it's also the case when you manually change the path in the browser's address bar. It turned out my auth service checked authorization for the url being navigated from in that case. |
…is triggered by the browser Fixed inconsistency in $location.path() behaviour on the $locationChangeStart event when using back/forward buttons in the browser or manually changing the url in the address bar. $location.path() now returns the target url in these cases. Closes angular#4989
…is triggered by the browser Fixed inconsistency in $location.path() behaviour on the $locationChangeStart event when using back/forward buttons in the browser or manually changing the url in the address bar. $location.path() now returns the target url in these cases. Closes angular#4989 Closes angular#5089 Closes angular#5118 Closes angular#5580
…is triggered by the browser Fixed inconsistency in $location.path() behaviour on the $locationChangeStart event when using back/forward buttons in the browser or manually changing the url in the address bar. $location.path() now returns the target url in these cases. Closes angular#4989 Closes angular#5089 Closes angular#5118 Closes angular#5580
Example plunk: http://plnkr.co/edit/DA3Oq6?p=preview
In a handler for the
$locationChangeStart
event, a call to$location.path()
will normally return the URL being navigated to. But if you use the forward or back button (or usehistory.back()
orhistory.forward()
) it will return the URL being navigated from.This isn't the case with the
$locationChangeSuccess
event, which behaves as expected.The text was updated successfully, but these errors were encountered: