Tri Vuong Tri Vuong - 3 years ago 189
AngularJS Question

AngularJS: avoid url encoding with $location

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

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



Is there a way to avoid the url encoding?

Answer Source

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

In short, if you intend on using $ 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 $ 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download