4

I'm looking for a way to get the host of the current URL using Angular2. So on

 http://example.com/myapp.html

I want to get:

 example.com

(in angular1 I used $location.host() to parse it out. Is there something similar?)

Reinhard
  • 1,516
  • 1
  • 18
  • 25
  • Possible duplicate of [How to get domain name for service in Angular2](http://stackoverflow.com/questions/36222845/how-to-get-domain-name-for-service-in-angular2) – Mark Rajcok Apr 26 '16 at 19:07

3 Answers3

9

Well, there's the old fashioned window.location JavaScript object. It has all of the elements that compose a url under it. Try printing it to the console to take a look:

console.log(window.location);

Edit:

What you actually need is window.location.host

See: http://www.w3schools.com/jsref/obj_location.asp

Shovalt
  • 6,407
  • 2
  • 36
  • 51
  • 2
    Accessing the window object is not recommended. Use [Location](https://v4.angular.io/api/common/Location) class. – mobby Mar 27 '18 at 14:12
1

window.location.*** it is as mentioned above. Here's the visual representation of different location properties: http://bl.ocks.org/abernier/3070589

rook
  • 2,819
  • 4
  • 25
  • 41
1

location.hostname For example, in your ts file, let x = //${location.hostname} and in your browser you type: http://www.example.com you get x="http://www.example.com" then you get example from x should be easy.

shevaroller
  • 103
  • 7
renzherl
  • 161
  • 1
  • 3