Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get base url in AngularJS

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/.

like image 652
alex Avatar asked Jan 30 '16 01:01

alex


Video Answer


3 Answers

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.

like image 73
Zach Nagatani Avatar answered Oct 16 '22 09:10

Zach Nagatani


Try this: $location.$$absUrl

console.log('$location',$location.$$absUrl);

btw /localhost is your base path, but I guess you meant entire URL.

like image 15
Leon Gaban Avatar answered Oct 16 '22 09:10

Leon Gaban


To get just the baseurl and nothing else use

$browser.baseHref()

it will return something like

/central/

like image 10
el-davo Avatar answered Oct 16 '22 09:10

el-davo