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.

1

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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?
According to angular-ui-router's documentation 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 and change the versions between the two and you can see the fix.
Upped to 0.2.13, seems to work. FWIW, .reload() also seems to work with that version as well. Thanks!
7

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

9 Comments

target="_self" not working... and where to put $window.location or $route.reload(), because its not working inside HomeController
you need to import the $window or the $route respectively into the controller in order to use them
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
Oh, I see. Did you try $state.go()? (it should use transitionTo internally, so I am not sure it will help..)
Yes, but it throws an error in console TypeError: Object #<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.
|
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});

Comments

-2

Try using:

$window.location.reload()

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.