Here on SO there's plenty of questions like this one but none of the answers helped me so far.
I've read the Angular $window and $location docs but I can't achieve what I want from my client app.
Let's say I've a webpage where the user can set multiple filters and share them, the sharing works through GET parameters ?f=AN_HASH
this triggers a query to my server which replies with the filter set matching the passed hash.
Active filters are stored in SessionStorage
(via $sessionStorage
from ngStorage
).
Eventually the user can clear current filter set via a button. The expected flow triggered by this button should be:
- Clear Session Storage item
- Clear the url query parameter
?f=
(I'd like to remove just this since there could be more than one parameter in the future) - Reload the page
This is the function called on ng-click
:
$scope.clearFilters = function(){
$sessionStorage.queryFilters = {} // Empty SessionStorage
$scope.activeFilters = false // Disable Clear button
console.log('before:', location.search)
// $location.search({})
// $location.search('f', null)
// $location.search('f', null).replace()
console.log('after:', location.search)
$window.location.reload() // reload the page
}
Between the two console.log
you can find what I've tried so far.
The two console logs prints the following:
before: ?f=THE_HASH
after: ?f=THE_HASH
apparently nothing has changed.. (also the GET query in the address bar is still there)
I tried by just emptying the location.search
object like this:
$scope.clearFilters = function(){
$sessionStorage.queryFilters = {} // Empty SessionStorage
$scope.activeFilters = false // Disable Clear button
location.search = '' // Reset location.search and reload page
}
This actually worked, but I don't like it since it removes ALL elements from the GET query and this could lead to problems in the future if I'll ever need to add more parameters which shouldn't been cleaned this way..
Other question that can provide background about my searches:
As @Chris suggested I tried launching:
angular.element(document.body).injector().get('$location').search("f",null)
The url GET hasn't been affected. In the attached image there's what i got from the Chrome console.