With $ location.search (): how to remove a parameter from a url when it is zero

$location.search() to set the url

the search parameter that is passed to search() is set by the form. selecting and deselecting an element causes some null parameter value, so the URL looks like this:

 myapp.com/search?type=text&parameter=null 

therefore, I would like to remove these "null" parameters from the url. The documentation has a "paramValue" that can be passed as the second parameter in .search (search, paramValue): if null , the parameter will be deleted.

but i cant do this job ... any suggestion?

edit:. This solution is based on @BKM's explanation.

to remove all parameters that are null , you must go through all of them and test each of them as follows:

 for (var i in search) { if (!search[i]) $location.search(i, null); } $location.search(search); 
+44
angularjs
Sep 02 '13 at 0:59
source share
3 answers

Using the $location service, you can remove the search parameter by setting it to zero:

In the case where your parameter is zero, in your case 'parameter' you can remove it from the URL by assigning it a null value, for example:

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

Hope this helps.

+87
02 Sep '13 at 5:14
source share

You can use $location.search({}) to clear everything at once.

+26
Jul 24 '15 at 7:57
source share

You can also do:

 var search = $location.search(); if (search.parameter == 'null') { $location.search({'parameter': null}); } 
-one
Aug 13 '17 at 14:14
source share



All Articles