Force AngularJS to reload a route although option reloadOnSearch is set to false
Asked Answered
C

3

15

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?

Coben answered 2/10, 2013 at 12:59 Comment(0)
K
16

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.

Kalevala answered 2/10, 2013 at 13:8 Comment(6)
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.Coben
Ah ok, now I got what you meant. I should listen to the $routeUpdate event and then reload when necessary.Coben
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.Kalevala
It would have been helpful that you included an exampleCalamity
function myController($route, $rootScope) { $rootScope.$on('$routeUpdate', function() { if (/*shouldReload*/) { $route.reload(); } } }Bartholomew
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 @Kalevala 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.Hardi
C
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>
Couchman answered 13/10, 2014 at 17:15 Comment(2)
Man! You made my day! This indeed is a hack, but it works beautifully and is sufficient for some limited needs. Thanks!Siple
This will reload the entire application not just the route. The non-hacky way is to use ui-sref-opts="{reload:true}"Willetta
C
1

This is also work

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

html

<div ng-click="reload()">reload</div>
Cuda answered 9/10, 2013 at 4:48 Comment(1)
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.Bothy

© 2022 - 2024 — McMap. All rights reserved.