Matheus Lima
Matheus Lima

Reputation: 2143

AngularJS change url with $location

I'm trying to change the URL with AngularJS, but not with a redirect, just change the URL after an event.

What I need is this:

www.myurl.com/inbox/1 to this www.myurl.com/inbox/25

In other words, just change the last Id.

I'm trying to do this:

$location.path('/inbox/'+id);

But what I'm getting is this:

www.myurl.com/inbox/1#/inbox/25

Upvotes: 24

Views: 60606

Answers (3)

David R
David R

Reputation: 11

For that you should use .url() instead of .path()

$location.url(`/inbox/${id}`)

Upvotes: 0

binariedMe
binariedMe

Reputation: 4329

Angular enforces the idea of one page web app. Browsers don't take any actions on change of anything after '#' value. So the best practice is to add variable attributes in url after '#' value which will keep the base url and attribute look clean on browser's address bar as well as solve your problem. My advice is to keep username, page no. , or any specific attribute id after '#' value. In your case you should use something like above said

www.myUrl.com/#/inbox/25

or

www.myUrl.com/inbox/#/25

Upvotes: 18

Orane
Orane

Reputation: 2251

usually angular URLs look like

www.myurl.com/#/inbox/1

in which case

$location.url('/inbox/25');

should work for you

Upvotes: 19

Related Questions