1

How to reload a controller and entire side using angular-ui-router?

$state.reload() or $state.go('myState')

does not work. Only method which works is:

$state.go('anotherState');
$state.go('myState');
  • going to another state and back. Then, view is reloaded corretly. How to do it without this hack?

Regards

5
  • Do you want page refresh or not? Commented Sep 19, 2017 at 10:52
  • It is SPA application so it should not. It should only reload controller like I said Commented Sep 19, 2017 at 10:53
  • When you say 'does not work', how do you know it does not reload? If you put an $onInit() in the state's controller and console logged in there, does it give you the message? If so, it's 'working'. Commented Sep 19, 2017 at 10:54
  • @rrd I have preloader which runs when page is loading first time, I see what is on scope and if controller is reloaded Commented Sep 19, 2017 at 10:58
  • Can you post your states so we can see them, might be an error in them in $state.reload() isn't working. Commented Sep 19, 2017 at 11:03

2 Answers 2

1

You can use this without page refresh. This will reload the view:

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

or else try this

$state.go($state.current.name,{},{reload: $state.current.name});
Sign up to request clarification or add additional context in comments.

2 Comments

Then you need to post your HTML and route files with the ui router version you are using
i have updated the answer. Please check it and try that
1

I found this to be the shortest working way to refresh with ui-router:

$state.go($state.current, {}, {reload: true}); //second param is for $stateParams if you have any.

New versions also support this:

$state.reload();  

Which is an alias for:

$state.transitionTo($state.current, $stateParams, { 
  reload: true, inherit: false, notify: true
});       // this is also used to reload the controller and view without page reload.

Documentation: https://angular-ui.github.io/ui-router/site/#/api/ui.router.state.$state#methods_reload

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.