Martijn
Martijn

Reputation: 24799

Why is Angular replacing my hashtag into %23?

When I redirect with $location.path(url), Angular replaces my # with %23:

var path = $location.path() + "/#" + categoryId;
$location.path(path);

How can I make sure Angular isn't replacing my hastag with %23?

Upvotes: 1

Views: 3698

Answers (2)

Ashan
Ashan

Reputation: 19748

This occurred to me when using $location.path('#/') for root. This happens because angular $location.path encodes the #tag if we append to the front in the path. Instead of $location.path('#/') using $location.path('') solves the problem for root.

for sub paths use $location.path('/something') where angular will append #tag and navigates to the url <base>/#/something

Upvotes: 0

Nicolas ABRIC
Nicolas ABRIC

Reputation: 4935

What you experience is normal. When you are using $location.path() to set a new path you do not want to use the # character as there is already one in your URL. If you do it anyway, as in your example, your # (the 2nd in the URL) will automatically be escaped because you can have only one unescaped # in an URL.

Long story short either you absolutely need this # before your categorId and then you would need to use decodeURIComponent when retrieving your path. Or more easy I would suggest that you don't use this character and replace your code by :

var path = $location.path() + "/" + categoryId;
$location.path(path);

Upvotes: 3

Related Questions