Angularjs 如何获得主机+;从角位置开始的基准

Angularjs 如何获得主机+;从角位置开始的基准,angularjs,Angularjs,我的angular应用程序位于目录“someApp”中。Url是http://example-domain/someApp/#/对于路径为:http://example-domain/someApp/#/user/login。我有一种“有角度的方式”来获得这个零件http://example-domain/someApp/ var redirectUri = $location.absUrl(); redirectUri = redirectUri.substr(0, redirectUri.i

我的angular应用程序位于目录“someApp”中。Url是
http://example-domain/someApp/#/
对于路径为:
http://example-domain/someApp/#/user/login
。我有一种“有角度的方式”来获得这个零件
http://example-domain/someApp/

var redirectUri = $location.absUrl();
redirectUri = redirectUri.substr(0, redirectUri.indexOf('#'));

最简单的方法是使用

window.location.origin + window.location.pathname
将返回
http://example-domain/someApp

这将提供整个基本url,即使使用虚拟目录/路径。 但是,IE不支持源代码。因此,您可以将url的组件连接起来,以提供基本目录,如下所示:

window.location.protocol + "//" + window.location.hostname + window.location.pathname
将返回
http://example-domain/someApp

如果使用虚拟目录,您将很难使用
$location
,因为
$location.path()
在散列之后返回,而
$location.host()
将只返回域,而不返回域和目录,这就是
window.location.hostname+window.location.pathname
提供给您的信息。

您需要使用:

 location.origin + location.pathname
假设url是http://example.com/#/some/path?foo=bar&baz=xoxo

$location.protocol(); // will give: http
$location.host();     // will give: example.com
location.host         // will give: example.com:8080
$location.port();     // will give: 8080
$location.path();     // will give: /some/path
$location.url();      // will give: /some/path?foo=bar&baz=xoxo

看完整的

我知道。如果我的url是
http://example.com/myApp/#/some/path?foo=bar&baz=xoxo
我想要
http://example.com/myApp
回答得很好,如果您使用非标准端口,我会使用window.location.host而不是hostname。