How do I use $location to to get the protocol host and port of my current url in a factory

Amanda Watson picture Amanda Watson · Aug 8, 2014 · Viewed 15.8k times · Source

How do I get the current protocol host and port of my current url?

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

I would like the url to look like this: http:// localhost:80 /somelocation

Answer

Fedaykin picture Fedaykin · Aug 9, 2014

You must inject the $location service, that's why it is undefined:

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