AngularJS ui-router: force reload

ぃ、小莉子 提交于 2019-12-30 01:41: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?


回答1:


The current trick is:

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




回答2:


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.




回答3:


This worked for me in a similar scenario.

$state.go('root.state').then(function(){
    $state.reload();
});



回答4:


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




回答5:


Try this:

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


来源:https://stackoverflow.com/questions/23257887/angularjs-ui-router-force-reload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!