I want to get the base path of my Angular app.
Right now I'm doing this: $scope.baseUrl = '$location.host()
But I only get this: /localhost. My current baseURL is http://localhost:9000/.
An alternative answer is using the $window service in conjunction with $location.absUrl() to create a new URL object, and then grab the origin via the origin property. This will give you exactly what you're looking for (minus the trailing '/').
$scope.baseUrl = new $window.URL($location.absUrl()).origin; will give you back http://localhost:9000 in your case.
Try this: $location.$$absUrl
console.log('$location',$location.$$absUrl);
btw /localhost is your base path, but I guess you meant entire URL.
To get just the baseurl and nothing else use
$browser.baseHref()
it will return something like
/central/
$location doesn't provide what the OP wanted.A cross browser solution that worked for me was:
$location.$$absUrl.replace($location.$$url, '')
$$absUrl is the entire url (e.g. https://google.com/search/spiders)
$$url is the part after the host (e.g. /search/spiders)
Modify to taste.
try injecting $location it has an absUrl() method on it that will return the entire current url
If you only want to get the base url of the site, and not the current url of the page, e.g. from https://www.google.com/somewhere/in-the-middle-of-nowhere you want to get https://www.google.com, then simply do:
// For www.google.com
$scope.baseUrl = $locaton.$$host;
// For https://www.google.com
$scope.baseUrl = $location.$$protocol + '://' + $location.$$host;
$location.port()absUrl()probably ? Check: docs.angularjs.org/api/ng/service/$location$scope.baseUrl = $location.protocol() + "://" + location.host;