Forced route reload / state by click

I'm currently doing something like this

link.on('click', function () { if (link.attr('href') !== $route.current.originalPath) return; $route.reload(); }); 

I don’t know about the side effects, but I think there might be something.

Is there an easier way to handle this in ngRoute, for example. through $location ?

What is the way to do the same in the UI Router when the application is updated to use it?

+5
source share
4 answers

Your code can be converted below $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 that $state.reload() reloading the state, but the controller does not get a duplicate. To do this, you need to use the code below instead of $state.reload()

 $state.transitionTo('tab.locations', $state.$current.params, { reload: true, inherit: true, notify: true }); 
+1
source

With the 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 is not. If the string should be "replaced", which will update the URL, and also replace the last history entry.
  • inherit - {boolean=true}, If true inherits url parameters from the current url.
  • relative - {object=$state.$current} , when going with a relative path (for example, '^') determines which state should relate to.
  • notify - {boolean=true} If true will broadcast events $ stateChangeStart and $ stateChangeSuccess.
  • reload (v0.2.5) - {boolean=false}, If true leads to a transition, even if the state or parameters have not changed, aka reload of the same state. It differs from reloadOnSearch because you will use it when you want to force a reboot when everything will be the same, including the Titles search.

Thus, we can force the state to reload:

 $state.go("stateName", stateParams, {reload: true}); 
+7
source

I found this to be the shortest way with a UI router:

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

or you can do this:

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

If you are using Ui.Router :

 link.on('click', function (event) { if (link.attr('href') !== $route.current.originalPath) return; event.preventDefault(); return $scope.$apply(function() { return $state.reload(); // $state.go($state.current.name, $stateParams, { inherit: false, reload: true, notify: true }); }); }); 

If you use just ngRoute , you must remove your current template from templateCache and return $ route.reload () to $ timout!

0
source

Source: https://habr.com/ru/post/1234881/


All Articles