epv epv - 1 year ago 71
AngularJS Question

How to set a url base based on your host

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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download