Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force reload route/state on link click

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?

like image 417
Estus Flask Avatar asked Oct 30 '15 18:10

Estus Flask


3 Answers

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 
});
like image 25
Pankaj Parkar Avatar answered Nov 13 '22 17:11

Pankaj Parkar


With UI-Router we have a set of options, and one of them is {reload: true}

go(to, params, options)

  • 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});
like image 192
Radim Köhler Avatar answered Nov 13 '22 19:11

Radim Köhler


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
     });
like image 1
Amit khanduri Avatar answered Nov 13 '22 19:11

Amit khanduri