AngularJS: How to clear query parameters in the URL?

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