Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove GET params with AngularJS?

#/order/123?status=success

hits the route '/order/:id' and takes to OrderCtrl.

Once we access the $routeParams.query, we want to clear the url to #/order/123. How can this be achieved?

like image 522
randomguy Avatar asked Jan 25 '13 17:01

randomguy


People also ask

What is URLSearchParams()?

The URLSearchParams interface defines utility methods to work with the query string of a URL.

What are URL search parameters?

URL parameters (known also as “query strings” or “URL query parameters”) are elements inserted in your URLs to help you filter and organize content or track information on your website. To identify a URL parameter, refer to the portion of the URL that comes after a question mark (?).


2 Answers

Using the $location service, you can remove the search param by assigning it a null value:

$location.search( 'status', null );

But you should note that, by default, this will reload the current route. If you don't want to reload the current route, you can disable that in your route definition:

$routeProvider.when( '/order/:id', {
  // yada yada...
  reloadOnSearch: false
});

But the $routeUpdate will still fire and can be reacted to.

like image 195
Josh David Miller Avatar answered Sep 22 '22 08:09

Josh David Miller


A more efficient way to remove all search params would probably be

$location.search({});
like image 36
yohairosen Avatar answered Sep 20 '22 08:09

yohairosen