如何使用$ location来获取工厂中当前URL的协议主机和端口

时间:2014-08-08 15:16:35

标签: angularjs location factory

如何获取当前网址的当前协议主机和端口?

app.factory('actionTypeFactory', ['$resource', function($resource, $location){
  return $resource($location.protocol() + '://'+ $location.host() +':'+  $location.port()  +'82/somelocation')
}]);

我希望网址看起来像这样:    http:// localhost:80 / somelocation

1 个答案:

答案 0 :(得分:16)

您必须注入$ location服务,这就是未定义的原因:

app.factory('actionTypeFactory', ['$resource', '$location', function($resource, $location){
  return $resource($location.protocol() + '://'+ $location.host() +':'+  $location.port()  +'82/somelocation')
}]);