29

I've seen these calls:

$location.url('/path/to/something/' + id + '/index');

and

$location.path('/path/to/something/' + id + '/index');

It seems they are doing the same thing.
From documentation I've found that URL is e.g. /path?a=b#hash, and path is a part of URL.

Is there any difference which of these setters to use ($location.path or $location.url) in case of the same argument?

1
  • not if you dont use search strings. They aren't included in path Commented Sep 17, 2015 at 15:16

2 Answers 2

47

$location.path returns the part of the URL after the slash NOT including search string parameters (after the question mark)

$location.url returns the entire URL after the slash, including search string parameters.

For example, let's say you have this URL

http://example.com/#/some/path?foo=bar&baz=xoxo

$location.url returns /some/path?foo=bar&baz=xoxo

$location.path returns /some/path

These two functions act as both setters and getters.

Url is basically Path + search strings. In your case, there are no search parameters, so both of them will return the same thing.

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

2 Comments

Thank you! Please note that I'm asking about setters.
Since you are using the same setters with no search parameters, there is no difference.
3

$location.path() allows you to extract out a specific part of your depending on your search string after the slash and excluding the parameters I guess. And if you don't have a search string then it works the same way as the $location.url() method does.

1 Comment

Thank you! Please note that I'm asking about setters.

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.