了解AngularJS中的域和上下文根url路径

Get to know domain and context root url path in AngularJS

本文关键字:上下文 url 路径 AngularJS 了解      更新时间:2023-09-26

我的angularJS应用程序运行在具有以下路径的web服务器中

http://www.some.domain.com/some/path/

注意,/some/path/是动态路径,因为我的应用程序可以部署到任何web服务器的任何目录。我需要在AngularJS中获得这个绝对的URL,不包括所有内部角度页面。例如,如果当前用户的页面是

http://www.some.domain.com/some/path/inner/angular/page.html

那么我要找的代码应该返回

http://www.some.domain.com/some/path/

不能使用$location,因为它只有关于当前SPA的信息。

您要查找的代码("/some/path/")是这样的:

var myContextWithPath = $window.location.pathname.substring(0, window.location.pathname.lastIndexOf("/"));

其他只返回上下文("/some")的变体是:

var myContext = $window.location.pathname.substring(0, window.location.pathname.indexOf("/",2));

您还可以获得原点("http://www.some.domain.com"):

var origin = $window.location.origin;

您可以为此使用$location不要忘记将$location添加到控制器功能

$location.host()

为您提供基本url。哪个是应用程序基础服务器域,如:www.example.com

$location.port()

给你的端口像:8080

$location.path()

提供您所在的位置:index.html