Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

with $location.search(): how to remove a parameter from the url when it's null

Tags:

angularjs

$location.search() to set an url

the search parameter which is passed to the search() is set by a form. selecting and deselecting an element causes some parameters value to be "null" and so the url looks like this :

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

so i would like to remove those "null" parameters from the url. In the documentation, there is a "paramValue" which can be passed as a second parameters to .search(search, paramValue) : If the value is null, the parameter will be deleted.

but i can't make this work… any suggestion ?

edit: here is a solution based on @BKM explanation

to remove every parameters which are null, it's necessary to loop through all of them and test each one, like this :

for (var i in search) {     if (!search[i]) $location.search(i, null); } $location.search(search); 
like image 264
François Romain Avatar asked Sep 02 '13 00:09

François Romain


People also ask

How do you remove parameters?

In the editor, select the parameters in the parameter line and choose Delete Parameters in the toolbar. The Delete and Adapt Parameters window is displayed, containing a list of parameters from which you can select the parameters you want to delete.

How do you represent null in URL?

A good choice for a "null" url is about:blank . The default behavior for browsers navigating to about:blank is to create an empty document in the current site context, without needing to make any web requests for the content, because there isn't any content.


1 Answers

Using the $location service, you can remove the search param by assigning it a null value:

In the case when your parameter is null, in your case 'parameter' you can remove it from the url by assigning it a null value like;

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

Hope it helps.

like image 177
BKM Avatar answered Oct 20 '22 17:10

BKM