1

Hi I am using angularjs $location service. How can I get root of my url using this service. like if my current location is

http://localhost:8080/xyz#123

I want to extract out http://localhost:8080 or at least localhost:8080

0

2 Answers 2

1
var protocol = $location.protocol();
var host = $location.host();
var port = $location.port();

var combined = protocol + '://' + host + ':' + port;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks I have implemented the fix. I am thinking to adding new method to $location service named getBaseUrl(), in app.run() method. Will that be right design.
If it's just for storing the data you might as well put it on $rootScope.
0

If you want to do it using only $location, you can use

$location.host() + ":" +  $location.port()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.