22

I have an AngularJS app starting at index.html and using ui-router. Based on a trigger I want to reload the complete page. I tried:

$state.go($state.current, $stateParams, {
    reload: true,
    inherit: false,
    notify: true
});

But that does not work. It doesn't reload my initial index.html.

I can do:

window.location.href = "index.html";

But then I'm on my initial page, not the current state.

Should a set window.location.href to index.html with query string parameters specifying current location? If I do that, how can I navigate to this location?

Serge van den Oever
  • 4,340
  • 8
  • 45
  • 66

5 Answers5

22

The current trick is:

$state.go($state.current.name, $state.params, { reload: true });

SamLeBarbare
  • 444
  • 4
  • 5
  • 2
    This doesn't work if your $state.go is the same $state(). It gets completely ignored and fails silently. $window.location.reload() does the trick though. Tested on 1.4x – RAC Nov 20 '15 at 02:27
18

Everybody seems to have ignored what Serge van den Oever appears to actually be asking, and that's that he wants entire page to reload, and not just the route.

The solution for that is pretty simple if you inject the $window service into your controller:

$window.location.reload();

If you just want the route to reload (which seems to be much more common), and are using ui-router, then just inject the $state service and do:

$state.reload();

This should reinitialize controllers now that the bug has been fixed, although I don't think it reinitializes resolves.

Mordred
  • 3,734
  • 3
  • 36
  • 55
  • 1
    Serge van den Oever stated that he want to reload the base html page ("...It doesn't reload my initial index.html..."). I think this answer should be accepted answer. – MÇT Jan 11 '17 at 19:28
1

This worked for me in a similar scenario.

$state.go('root.state').then(function(){
    $state.reload();
});
James Parker
  • 2,095
  • 3
  • 27
  • 48
0

If you include the $route service in your controller you could try $route.reload();

hydrogen
  • 2,878
  • 21
  • 20
  • Hi hydrogen, problem is that I am using the superior ui-router routing system for AngularJS (http://angular-ui.github.io/ui-router/site/#/api/ui.router), not the built in AngularJS routing system. – Serge van den Oever Apr 24 '14 at 09:18
  • Ah I see, in this case you might need to try: `$state.reload()` in order to get it to reload. `ui.router` has a $state service you can inject to do this. – hydrogen Apr 28 '14 at 03:11
  • Problem is $state.reload() doesn't initialize controllers again. There's open ticket for that https://github.com/angular-ui/ui-router/issues/582 – WTK May 06 '14 at 06:01
0

Try this:

$state.transitionTo($state.current, $stateParams, {
    reload: true,
    inherit: false,
    notify: true
});
Aurelio
  • 24,702
  • 9
  • 60
  • 63
Albestio
  • 114
  • 1
  • 4