4

AngularJS truncates the trailing equal signs when routing to them. I have a base64 string which need to be added as a query parameter to the url(route)

ex: http://example.com/#!/updatepassword?code=NnuW3q49QW38Mf-Cg==

this loads as: http://example.com/#!/updatepassword?code=NnuW3q49QW38Mf-Cg

Is there a workaround for this?

Chathuranga
  • 1,008
  • 1
  • 15
  • 26

1 Answers1

3

If you try to set route as a string, then you need to escape = sign. That's because this character has a special meaning in the query string - it separates parameter name from its value.

So, one solution could be:

var query = "code=NnuW3q49QW38Mf-Cg==";
$location.url("/some/path?" + encodeURIComponent(query));

What encodeURIComponent() will do, is it will replace all special characters - = will be replaced with %3D for instance. This will prevent it from being interpreted as a key-value separator.

If you only want to change the query string parameters, not the whole URL, you can also use $location.search() method:

$location.search("code", "NnuW3q49QW38Mf-Cg==");

Just remember to pass two parameters to that method, not one. If you do:

$location.search("code=NnuW3q49QW38Mf-Cg==");

the = sign will not get escaped, only stripped.


Angular uses parseKeyValue() function internally to parse query string, it can be found here. You can see that the split is being done over the = sign. That's why they get stripped.

But if you take a look at .search() method implementation, you see that parseKeyValue() is being called only if you supply one argument to .search(). It's not invoked if you supply name of the parameter as a first, and value as a second argument.

Peeking at the source code also suggests yet another solution:

$location.search({"code": "NnuW3q49QW38Mf-Cg=="});
kamituel
  • 34,606
  • 6
  • 81
  • 98