2

I need to get base URL using angular.js

Can any one help me on this?

I tried with different solutions and but I am not getting proper URL.

2
  • 1
    What have you tried so far? Are you injecting $location into your controller? Tried $location.$$absUrl? Commented Dec 15, 2016 at 15:06
  • 3
    Possible duplicate of How to get URL segment in Angular JS? Commented Dec 15, 2016 at 15:18

2 Answers 2

1

The angular way of getting the url is using the $location service. You need to inject it wherever you need it (controller, another service, etc...)

angular
    .module('app')
    .controller('controller', mainController);

/** @ngInject **/
function mainController($location){
  console.log($location) //prints all $location properties
  console.log($location.$$host) // prints 'stackoverflow.com'
  console.log($location.$$absUrl) // prints 'http://stackoverflow.com/exampleurl'
}

You can do it the non-angular way, using the global window.location object:

console.log(location.href) // prints 'http://stackoverflow.com/exampleurl'

Hope it helps

Sign up to request clarification or add additional context in comments.

1 Comment

How do i get the same using AngularJS 6+ ?
0

If you want to get the base URL in the browser using Javascript you can use:

window.location.origin

Or to get an object of all parts use:

window.location

Hope this helps :)

1 Comment

Thanks for your info.

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.