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

时间:2022-08-24 12:51:12

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.

他们似乎在做同样的事情。从文档中我发现URL是e.g. /path?路径是URL的一部分。

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

这些setter中使用哪一个($location)有区别吗?在相同的参数下,路径或$location.url) ?

2 个解决方案

#1


39  

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

美元的位置。路径返回在斜杠后面的URL部分,不包括搜索字符串参数(问号后面)

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

美元的位置。url返回斜杠后的整个url,包括搜索字符串参数。

For example, let's say you have this URL

例如,假设您有这个URL

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

http://example.com/ /一些/路径? foo = bar baz =亲亲抱抱

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

美元的位置。url返回/一些/路径? foo = bar baz =亲亲抱抱

$location.path returns /some/path

美元的位置。路径返回/一些/路径

These two functions act as both setters and getters.

这两个函数同时充当setter和getter。

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

Url基本上是路径+搜索字符串。在你的例子中,没有搜索参数,所以它们都会返回相同的东西。

#2


2  

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

$location.path()允许您在斜杠和不包括参数的情况下提取您的特定部分,这取决于您的搜索字符串。如果没有搜索字符串,那么它的工作方式与$location。url()方法相同。

#1


39  

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

美元的位置。路径返回在斜杠后面的URL部分,不包括搜索字符串参数(问号后面)

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

美元的位置。url返回斜杠后的整个url,包括搜索字符串参数。

For example, let's say you have this URL

例如,假设您有这个URL

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

http://example.com/ /一些/路径? foo = bar baz =亲亲抱抱

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

美元的位置。url返回/一些/路径? foo = bar baz =亲亲抱抱

$location.path returns /some/path

美元的位置。路径返回/一些/路径

These two functions act as both setters and getters.

这两个函数同时充当setter和getter。

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

Url基本上是路径+搜索字符串。在你的例子中,没有搜索参数,所以它们都会返回相同的东西。

#2


2  

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

$location.path()允许您在斜杠和不包括参数的情况下提取您的特定部分,这取决于您的搜索字符串。如果没有搜索字符串,那么它的工作方式与$location。url()方法相同。