How to set query parameter in AngularJS without executing a route?

My Angular application allows the user to load the element, and when this happens, I would like to set the query string containing the identifier of the element, so that if the user refreshes the page (or wants to link it), the URL is already set (there is already code that loads the element, if the identifier is present in $ routeParams).

How can I set this query parameter without calling a route? There are other things on the page that will get reset (including a plugin that will have to reload all its data) if the route happens, so it’s important that only the request parameter changes.

In short, when I load element 123, all I want is that the URL changes from:

www.myurl.com/#/Elements

at

www.myurl.com/#/Items? ID = 123

without any routing.

What is the best way to do this?

+68
angularjs url routing
Jul 07 '13 at 14:38
source share
1 answer

In the $ routeProvider reloadOnSearch select false:

 $routeProvider .when('/items', { controller: 'ItemsCtrl', templateUrl: '/templates/items', reloadOnSearch: false }, ... ); 

and in your controller use $location.search() to set the id parameter:

 $location.search('id', 123); 
+158
Jul 07 '13 at 15:50
source share



All Articles