Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: How to clear query parameters in the URL?

Tags:

angularjs

People also ask

How do I remove values from a URL?

Just pass in the param you want to remove from the URL and the original URL value, and the function will strip it out for you. To use it, simply do something like this: var originalURL = "http://yourewebsite.com?id=10&color_id=1"; var alteredURL = removeParam("color_id", originalURL);

How do you clear a query string?

The only way to 'clear' the query string is to do a redirect to the same URL but without the query string part. Is there particular reason why you want it cleared? If security is the issue, try using a POST call instead or encrypt the query string.


I use

$location.search('key', null)

As this not only deletes my key but removes it from the visibility on the URL.


I ended up getting the answer from AngularJS forum. See this thread for details


The link is to a Google Groups thread, which is difficult to read and doesn't provide a clear answer. To remove URL parameters use

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

To remove ALL query parameters, do:

$location.search({});

To remove ONE particular query parameter, do:

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

To clear an item delete it and call $$compose

    if ($location.$$search.yourKey) {
        delete $location.$$search.yourKey;
        $location.$$compose();
    }

derived from angularjs source : https://github.com/angular/angular.js/blob/c77b2bcca36cf199478b8fb651972a1f650f646b/src/ng/location.js#L419-L443