gpt4 book ai didi

angularjs - 如何从 Angular $location 获取主机 + 基数

转载 作者:行者123 更新时间:2023-12-03 16:54:02 27 4
gpt4 key购买 nike

我有位于目录“someApp”中的 Angular 应用程序。网址是 http://example-domain/someApp/#/对于一些带有路径的状态 url 是:http://example-domain/someApp/#/user/login .我有“Angular 方式”来获得这部分 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给你。

关于angularjs - 如何从 Angular $location 获取主机 + 基数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32464544/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com