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?
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.
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.
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.
$http is an AngularJS service for reading data from remote servers.
$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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With