5

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?

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Some years late, but I have found a way to bypass the URI encode made by the $location service: just rewrite the window.encodeURIComponent

var original = window.encodeURIComponent;
window.encodeURIComponent = function (str) {
   return decodeURI(str);
};
$location.url('/somePath?ids=[1,2,3]');
window.encodeURIComponent = original;

It is not the best solution, but it works as intended.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.