Home » Angularjs » Removing the querystring in AngularJS

Removing the querystring in AngularJS

Posted by: admin November 30, 2017 Leave a comment

Questions:

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.

Answers:

Try this:

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

See documentation for more information.

Questions:
Answers:

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.

Questions:
Answers:

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

$location.search('')

Questions:
Answers:

try this

$location.url('/');

See documentation for more information.