Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取AngularJS中的基本url_Javascript_Angularjs - Fatal编程技术网

Javascript 获取AngularJS中的基本url

Javascript 获取AngularJS中的基本url,javascript,angularjs,Javascript,Angularjs,我想得到我的Angular应用程序的基本路径 现在我正在这样做:$scope.baseUrl='$location.host() 但我只知道:/localhost。我当前的基本URL是http://localhost:9000/尝试注入$location它有一个absUrl()方法,该方法将返回整个当前url 试试这个:$location.$$absUrl console.log(“$location”,“$location.$$absUrl”) 顺便说一句,/localhost是您的基本路径,

我想得到我的Angular应用程序的基本路径

现在我正在这样做:
$scope.baseUrl='$location.host()


但我只知道:
/localhost
。我当前的基本URL是
http://localhost:9000/

尝试注入
$location
它有一个
absUrl()
方法,该方法将返回整个当前url


试试这个:
$location.$$absUrl

console.log(“$location”,“$location.$$absUrl”)


顺便说一句,
/localhost
是您的基本路径,但我猜您指的是整个URL。

要仅获取基本URL而不获取其他内容,请使用

$browser.baseHref()
它将返回类似于


/central/

而不是使用
$scope.baseUrl='$location.host()

使用这个:-

   $scope.baseUrl = $location.absUrl();

如果您只想获取网站的基本url,而不是页面的当前url,例如从
https://www.google.com/somewhere/in-the-middle-of-nowhere
您想要获得
https://www.google.com
,然后只需执行以下操作:

// For www.google.com
$scope.baseUrl = $locaton.$$host;
// For https://www.google.com
$scope.baseUrl = $location.$$protocol + '://' + $location.$$host;

另一种解决方法是将
$window
服务与
$location.absUrl()
结合使用,创建一个新的URL对象,然后通过origin属性获取原点。这将为您提供所需的内容(减去尾随的“/”)


$scope.baseUrl=new$window.URL($location.absUrl()).origin将在您的情况下返回给您

对我有效的跨浏览器解决方案是:

$location.$$absUrl.replace($location.$$url, '')
$$absUrl
是整个url(例如)

$$url
是主机后面的部分(例如/search/spider)


修改以适应口味。

我参加聚会迟到了,但是我认为
位置.路径名可以更好。

您还需要
$location.port()
absUrl()
?选中:
$scope.baseUrl=$location.protocol()+“:/”+location.host$location
不提供OP想要的内容。您不应该使用以$$开头的属性和函数,而应该找到一种受支持的角度方式。例如,使用absUrl()方法