AngularJS中的$ location.path(path)与$ location.url(url) [英] $location.path(path) vs $location.url(url) in AngularJS

查看:321
本文介绍了AngularJS中的$ location.path(path)与$ location.url(url)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过这些电话:

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

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

似乎他们在做同样的事情.
从文档中我发现URL例如/path?a=b#hash,并且路径是URL的一部分.

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.

在使用相同参数的情况下,使用哪个设置器有何区别($location.path$location.url)?

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

推荐答案

$location.path返回斜线后的URL部分,其中不包括搜索字符串参数(问号之后)

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

$location.url返回斜杠后的整个URL,包括搜索字符串参数.

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

例如,假设您有此URL

For example, let's say you have this URL

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

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

$location.path返回/some/path

这两个函数同时充当settersgetters.

These two functions act as both setters and getters.

Url基本上是Path + search strings.在您的情况下,没有搜索参数,因此它们都将返回相同的内容.

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

这篇关于AngularJS中的$ location.path(path)与$ location.url(url)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆