Mark Mark - 2 months ago 21
Javascript Question

Replace URL in Angular environment

I need to modify the URL after opening a new window and navigating to a state.

My starting page looks like this:


When it loads the URL becomes


In the new window I am navigating to a new state:


What I want is to have the URL looking like this:


I am getting almost correct results except that instead of replacing the URL completely it adds


So the result is something like this:


while what I need is:


Here is some relevant code:

if ($window.history.replaceState) {
var newUrl = '?project=903' + '#/case';

Seems to me I am having two problems. Extra '#/' after '.html' and the URL is encoded. Because even if I remove extra '#/' it still does not work unless I replace encoded characters with real ones.
Please help.



I think you are using the wrong method. You want to be using $location.url which will allow you to set the path, query, and hash values all at once. But you may be better off setting the query and hash separately because you don't need to change the entire url.

$'project', '903');

I removed the / from the hash because it isn't necessary.

Also, by default angular uses hash mode to navigate its routes. If you want to supply a hash I think you will need to turn that behavior off.


Set that in your application's configuration.

Here is a good article: