【发布时间】:2019-01-30 13:02:10
【问题描述】:
我想获取当前位置的基本 URL,但是我有 2 个场景。
http://localhost:6111/Page.aspx 基本网址:localhost:6111
http://localhost/myApp/Page.aspx 基本网址:localhost/myApp
我希望能够有一种解决方案来检索两个基本 URL。 大家有什么推荐吗?
谢谢。
【问题讨论】:
标签: angularjs
我想获取当前位置的基本 URL,但是我有 2 个场景。
http://localhost:6111/Page.aspx 基本网址:localhost:6111
http://localhost/myApp/Page.aspx 基本网址:localhost/myApp
我希望能够有一种解决方案来检索两个基本 URL。 大家有什么推荐吗?
谢谢。
【问题讨论】:
标签: angularjs
这是你应该做的事情
获取整个路径,然后排除文件部分
let absUrl = $location.absUrl();
let expected = absUrl.replace('Page.aspx', '');
或者如果文件名一直不一样
let expected = absUrl.replace(/([a-z]+\.[a-z]+)/i, '');
【讨论】:
对于第一种情况,您可以这样做(如$location API 建议的那样):
// url -> http://localhost:6111/Page.aspx
let host = $location.host();
// host => localhost:6111
对于第二种情况,我认为您需要同时使用 $location.host() 和 $location.url()。然后操纵 url 只获取第一部分。
// url -> http://localhost/myApp/Page.aspx
let host = $location.host(); // localhost
let url = $location.url(); // /myApp/Page.aspx
let splittedUrl = url.split('/'); // ["", "myApp", "Page.aspx"]
let resultBaseUrl = host + "/" + splittedUrl[1]; // localhost/myApp
我不完全确定第二个解决方案,测试它并给我反馈!
【讨论】: