8

I am using angular ui-router. It has state in each routing, but I am not able to reload the state. Here are my code

app.config(function ($urlRouterProvider, $stateProvider) {
$urlRouterProvider.otherwise("/home");
$stateProvider
    .state('home', {
        url: "/home",
        controller: 'HomeController',
        views: {
        "viewHome": {
            templateUrl: "home.html"
        },

    })
}

in HTML

<a href="#/home">Home</a>

When I clicked on Home link then it redirected to home url but when I was trying to click again the home link then its not refresh the same state, It must be refresh the home state and again call the HomeController. Please give me some solution for this.

Anil Kumar Pandey
  • 1,877
  • 4
  • 27
  • 44
  • May be this topic hepl you [http://stackoverflow.com/questions/11267284/angularjs-refresh-when-clicked-on-link-with-actual-url][1] [1]: http://stackoverflow.com/questions/11267284/angularjs-refresh-when-clicked-on-link-with-actual-url – iKBAHT Sep 27 '13 at 10:25

4 Answers4

8

Using angular-ui-router I'm successfully reloading a state with:

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

This reloads the state and re-inits the controller.

blockloop
  • 5,565
  • 5
  • 30
  • 31
  • I don't believe this re-inits the controller. It just seems to reload the state...this seems to have the same problem as simply calling $state.reload(); Is there something I am missing? – jrista Mar 23 '15 at 20:06
  • According to [angular-ui-router's documentation](https://github.com/angular-ui/ui-router/wiki/Quick-Reference#statereload) the controllers are supposed to reinstantiate. There was a bug for some time, but by testing versions 0.2.11 and 0.2.10 of angular-ui-router the bug seems to be fixed in v0.2.11. [See here](http://plnkr.co/edit/snd3Cf?p=preview) and change the versions between the two and you can see the fix. – blockloop Mar 23 '15 at 21:41
  • 1
    Upped to 0.2.13, seems to work. FWIW, .reload() also seems to work with that version as well. Thanks! – jrista Mar 23 '15 at 21:57
7

Try adding target="_self" to the a. You can also use $window.location. Also $route.reload() should do the trick.

fusio
  • 3,595
  • 6
  • 33
  • 47
  • target="_self" not working... and where to put $window.location or $route.reload(), because its not working inside HomeController – Anil Kumar Pandey Aug 19 '13 at 06:51
  • you need to import the `$window` or the `$route` respectively into the controller in order to use them – fusio Aug 19 '13 at 07:14
  • Thanks for suggestions @fusio but its not working. May be its because I am using ui-router and it has state provider, I have also tried `$state.transitionTo("home")` but its also not working any more – Anil Kumar Pandey Aug 19 '13 at 09:59
  • Oh, I see. Did you try `$state.go()`? (it should use `transitionTo` internally, so I am not sure it will help..) – fusio Aug 19 '13 at 10:05
  • Yes, but it throws an error in console `TypeError: Object # has no method 'go'` its because $state.go() is available in Latest alpha 0.0.2 version and its not a stable version thats why I am using previous version 0.0.1. – Anil Kumar Pandey Aug 19 '13 at 10:22
  • The [doc](https://github.com/angular-ui/ui-router/wiki/Quick-Reference#stategoto--toparams--options) says it sets `options = { location: true, inherit: true, relative: $state.$current }` so try with that.. I have no other idea, sorry – fusio Aug 19 '13 at 10:26
  • I think `$state.go()` is also not available in 0.0.2 version. – Anil Kumar Pandey Aug 19 '13 at 10:27
  • $state.go() doesn't seem to reload the view if you're there already? – chrismarx Sep 27 '13 at 16:06
  • @fushio: You saved me a day. target self works like a charm. Page Name – Telvin Nguyen Jan 17 '17 at 09:39
2

You can give which state you want to call it's will reload your page and controller. This code is working for me:

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

or

$state.go('state.name',{},{reload:true});
uhbif19
  • 3,139
  • 3
  • 26
  • 48
-2

Try using:

$window.location.reload()

Reloads the content of the page. Type this line inside any controller you want to have it in.