Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$location.path(path) vs $location.url(url) in AngularJS

Tags:

url

angularjs

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?

like image 457
naXa Avatar asked Sep 17 '15 15:09

naXa


People also ask

What is location path in AngularJS?

The $location in AngularJS basically uses window. location service. The $location is used to read or change the URL in the browser and it is used to reflect that URL on our page. Any change made in the URL is stored in the $location service in the AngularJS.

What is the use of $location service in AngularJS?

The $location service parses the URL in the browser address bar (based on the window. location) and makes the URL available to your application. Changes to the URL in the address bar are reflected into $location service and changes to $location are reflected into the browser address bar.

Which method of $location service is used to get the full URL of the current web page?

By using $location service in angularjs we can get information like full url of current web page, part of url of web page, protocol of web page, host information of current url and port of current url.

What is $HTTP in AngularJS?

$http is an AngularJS service for reading data from remote servers.


1 Answers

$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.

like image 171
Richard Hamilton Avatar answered Sep 20 '22 04:09

Richard Hamilton