14

In the route configuration of my AngularJS application most of the routes are defined with the option reloadOnSearch set to false as most of the time a page should not reload when search parameter changed.

But in rare circumstances I need a different behaviour where I need to reload the URL although only the search parameters changed.

Is there a way to force AngularJS to reload the URL although reloadOnSearch is set to false?

Flo
  • 27,355
  • 15
  • 87
  • 125

3 Answers3

15

I have not tried it, but something that you can try is.

In the $routeUpdate event handler call the $route.reload() method if you know the condition that should cause refresh of route.

Chandermani
  • 42,589
  • 12
  • 85
  • 88
  • What do you mean with $routeUpdate event handler, how can I configure its behaviour or is it part of the AngularJS core? I don't want to change the framework itself to be able to update the version later. – Flo Oct 02 '13 at 13:16
  • Ah ok, now I got what you meant. I should listen to the $routeUpdate event and then reload when necessary. – Flo Oct 02 '13 at 14:58
  • 1
    Basically you can do $route.reload() anytime you want. When to do is upto you. Since you want to refresh the view in a case when the search query changes, right. How would you know that search query has changed. This event is raise when search query is updated. – Chandermani Oct 02 '13 at 15:22
  • 3
    It would have been helpful that you included an example – Jago Jun 05 '14 at 03:28
  • 1
    `function myController($route, $rootScope) { $rootScope.$on('$routeUpdate', function() { if (/*shouldReload*/) { $route.reload(); } } }` – Clay Mar 20 '15 at 14:57
  • Short hint. If you have _a concrete point in the code_ where you are adding search params to the URL, but they are not provoking a reload because of having `reloadOnSearch: false` for that route, as @Chandermani said you can execute `$route.reload()` wherever you need it. So just include the `$route` service as a dependencie of your class and execute `$route.reload()` on that concrete point you need it. – Alex MM Dec 21 '16 at 09:48
13

In case you are trying to force reload when a user clicks a link, set link's target attribute. It will trigger reload regardless of reloadOnSearch=false

<a href="/same_page?different_param=123" target="_self">Reload</a>
zavidovych
  • 4,718
  • 2
  • 24
  • 22
  • Man! You made my day! This indeed is a hack, but it works beautifully and is sufficient for some limited needs. Thanks! – piotr.d Dec 09 '14 at 16:38
  • 1
    This will reload the entire application not just the route. The non-hacky way is to use ui-sref-opts="{reload:true}" – BtnMike Mar 03 '17 at 14:22
1

This is also work

app.controller('appCtrl',function($scope,$window){
           $scope.reload =function(){
               $window.location.reload(); 
           }
})

html

<div ng-click="reload()">reload</div>
Nishchit
  • 18,284
  • 12
  • 54
  • 81
  • 4
    Although question was not formed in such a way to clarify whether he/she wants to reload the route or the whole application, I am pretty sure that he wants to reload the route. Your solution re-initializes the whole application. – epitka Sep 10 '14 at 16:34