Currently I'm doing something like this
link.on('click', function () {
if (link.attr('href') !== $route.current.originalPath)
return;
$route.reload();
});
I'm not aware of side effects but I guess there can be some.
Is there more straightforward way to handle this in ngRoute, e.g. through $location
?
What is the way to do the same thing in UI Router when the app will be updated to use it?
Your code could get tranformed to below change $route.reload()
to $state.reload()
Code
link.on('click', function () {
if (link.attr('href') !== $route.current.originalPath)
return;
//though this will not reload the controller content
$state.reload(); //will force to reload state..
$scope.$apply(); //needed here to tell angular js to run digest cycle.
});
From git-hub issue it seems like $state.reload()
reload the state but controller doesn't get re-instantiate. For that you need to use below code instead of $state.reload()
$state.transitionTo('tab.locations', $state.$current.params, {
reload: true, inherit: true, notify: true
});
With UI-Router we have a set of options, and one of them is {reload: true}
location - {boolean=true|string=}
- If true will update the url in the location bar, if false will not. If string, must be "replace", which will update url and also replace last history record.inherit - {boolean=true},
If true will inherit url parameters from current url.relative - {object=$state.$current}
, When transitioning with relative path (e.g '^'), defines which state to be relative from.notify - {boolean=true}
, If true will broadcast $stateChangeStart and $stateChangeSuccess events.reload (v0.2.5) - {boolean=false},
If true will force transition even if the state or params have not changed, aka a reload of the same state. It differs from reloadOnSearch because you'd use this when you want to force a reload when everything is the same, including search params.
So we can force state reload with:
$state.go("stateName", stateParams, {reload: true});
I found this to be the shortest way with UI-router :
$state.go($state.current, {}, {reload: true});
or you can do this :
$state.transitionTo($state.current, $stateParams, {
reload: true,
inherit: false,
notify: true
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With