1

I want to be able to set a var urlbase based on my host. For example, I want to say in my service call,

    if (host ="localhost:8000") {
        var urlBase = '/api';
    } else {
        var urlBase = '/core/api';
    }

What would I put in the "Host" to check the host of the website?

enavuio
  • 1,428
  • 2
  • 19
  • 31

5 Answers5

4

The hostname is available on the location global variable.

Your code might look like this:

var host = location.hostname;

I should also note that your code isn't going to work as written. Your if statement is assigning a value to host with =, rather than comparing it with ==.

More information about hostname at W3Schools and this question. See lots of details about the location variable at MDN.

AngularJS also offers an injectable service to allow for testing called $location that does all the same things.

Community
  • 1
  • 1
deltree
  • 3,756
  • 1
  • 30
  • 51
3

In AngularJS you have the $location service

https://docs.angularjs.org/api/ng/service/$location

check for absUrl(), host(), and url() methods.

var host = $location.host();
1

Use the $location service provided by angular https://docs.angularjs.org/api/ng/service/$location

function MyController($location){

    var host = 'http://' + $location.host();

}
Mohit Yadav
  • 156
  • 1
  • 8
1

You can do this.

if(window.location.hostname == 'localhost')
    var urlBase = '/api';
else
    var urlBase = '/core/api';
pritesh
  • 2,162
  • 18
  • 24
0

we can inject $state in serice where we can see url an you can set base url