Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: avoid url encoding with $location

Tags:

angularjs

I notice that when I passed a parameter that is an array to $location.search(), it was encoded as in the following example

$location.path('/somePath').search('ids[]=', [1, 2, 3]);

becomes

/somePath?ds%5B%5D=1&ds%5B%5D=2&ds%5B%5D=3

Is there a way to avoid the url encoding?

like image 734
trivektor Avatar asked Oct 21 '22 16:10

trivektor


1 Answers

Excuse the late reply, but I struck a similar predicament myself and thought I'd offer advice.

In short, if you intend on using $location.search you cannot avoid the URL being encoded.

If you take a look at the Angular source, location.js specifically, you'll notice that the functions return composed URLs (i.e. LocationHtml5Url) all rely on another function call named toKeyValue, which will encode all key-value pairs whenever they are set.

Furthermore, in the example use case you've provided, you don't need the equals inside your key. When $location.search is called with two parameters, they are treated as a key-value pair by Angular.

If you need to make use of the location URL after it has been encoded, you could always call decodeURIComponent.

like image 196
miqh Avatar answered Jan 02 '23 21:01

miqh