Skip to content

$stateChangeSuccess fires when url still has the previous state in it. #1151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
SimplGy opened this issue Jun 20, 2014 · 4 comments
Closed

Comments

@SimplGy
Copy link

SimplGy commented Jun 20, 2014

UI-Router Question

Inside of $stateChangeSuccess, the current window.location.hash is still the previous state. This is not what I expect (since I'm in the success handler). Is this known? Is it a mindful decision?

$rootScope.$on('$stateChangeSuccess', function (event, toState, toParams) {
  console.log window.location.hash // '#/myLastPage' <-- I expect this to be changed already.
  toState.name // 'myNextPage'  
  setTimeout(function(){
    console.log(window.location.hash) // '#/myNextPage'
  }, 1)
});

Background (you can ignore this if you like)

I need to force a real, complete page refresh in some situations to relieve pressure from built-up memory leaks.

An approach to do this is to watch the state changes, and do a full page refresh every 100th or so.

$state.reload() doesn't do a full reload. Neither does $state.go($state.current, null, {reload: true}). Neither does window.location.reload(false) (it's supposed to skip the cache, but actually doesn't re-parse the JS, which is a shame).

So, I'm using window.location.reload(true) to force a refresh. This uses the value of window.location at the time, so using $stateChangeSuccess requires this setTimeout.

@Skinner927
Copy link

Use the $location service. You'll prob want $location.absUrl().

@eddiemonge
Copy link
Contributor

@nolazybits
Copy link

I would like to reopen this issue as I can't use any angular related thing in the component I'm using.
I would have hoped we could use window.location...

@ProdigySim
Copy link

ProdigySim commented Aug 5, 2016

I ran into this issue because my controller wants to save data to history.state. However, at the time the controller boots up, the URL is not correctly pointing at the current state's URL--so I cannot correctly access the history entry for the current page.

I thought perhaps I could listen to this event, but it seems that the event fires even before the controller is run.

I had the choice of either using $timeout or the $locationChangeSuccess to execute code after the URL was updated. This seems to work, but it would be nice if the History API state and ui router state were more in sync.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants