我有角度应用程序位于目录'someApp'.Url http://example-domain/someApp/#/用于某些状态url,路径为:http://example-domain/someApp/#/user/login.我有"棱角分明的方式"来获得这一部分http://example-domain/someApp/
var redirectUri = $location.absUrl();
redirectUri = redirectUri.substr(0, redirectUri.indexOf('#'));
Run Code Online (Sandbox Code Playgroud)
最简单的方法是使用
window.location.origin + window.location.pathname
Run Code Online (Sandbox Code Playgroud)
哪会回来 http://example-domain/someApp
这将提供整个基本URL,即使使用虚拟目录/路径也是如此.但是,IE不支持原点.因此,您可以连接url的组件以提供基本目录,如下所示:
window.location.protocol + "//" + window.location.hostname + window.location.pathname
Run Code Online (Sandbox Code Playgroud)
哪会回来 http://example-domain/someApp
如果使用虚拟目录,您将很难使用$location,因为$location.path()返回哈希后$location.host()只会返回域,而不是域和目录,这就是window.location.hostname + window.location.pathname给你的.
您需要使用:
location.origin + location.pathname
Run Code Online (Sandbox Code Playgroud)
假设网址是 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
Run Code Online (Sandbox Code Playgroud)
查看完整详细信息