Force reload route/state on link click

Issue

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?

Solution

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 
});

Answered By – Pankaj Parkar

Answer Checked By – Clifford M. (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.