23

After reading and processing a query-string value from the URL, for example

http://myurl.com/#/?foo=baa

I can change the URL to

http://myurl.com/#/?foo=

by using

$location.search('myQueryStringParameter', '');

How do I get rid of the query-string altogether (without explicit redirects or server side action and so on) so that only

http://myurl.com/#/

remains in the browser? It should be fairly simple, but I can't find any reference.

4 Answers 4

46

You were close, you needed to set null

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

From ng.$location documentation

If search is a string, then paramValue will override only a single search parameter. If paramValue is an array, it will set the parameter as a comma-separated value. If paramValue is null, the parameter will be deleted.

Sign up to request clarification or add additional context in comments.

4 Comments

This for some reason is deleting all my parameters. If I have name=Olly&position=forward and I call $location.search(position, null), everything gets deleted.
@Alessandro if you can create a test project that reproduces it log it as a bug. It has worked as documented for me for a long (in Angular land) time now.
@toxaq but it reloads the page
@saf that's true of any change to $location.search you need reloadOnSearch:false in your route (if using default routing).
28

Try this:

$location.url($location.path())

See documentation for more information.

4 Comments

but it reloads the page
This clears all querystring key/value-s. What if I just want to remove only one of them?
@saf don't forget to set reloadOnSearch:false in your route
It gives error - 'transition superseded' in the console.
9

The easiest solution is to do the same as internaly in $location.url

$location.search('')

Comments

-2

try this

$location.url('/');

See documentation for more information.

1 Comment

This would redirect the user to the root path and that's NOT what the OP wanted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.