Gal Hadas
Gal Hadas

Reputation: 69

how to get the domain name with router in Angular 7

I'm trying to get the domain with the Router through ActivatedRouter but I can't get the domain.

Is there a way to get the domain name from one of them?

Upvotes: 2

Views: 10453

Answers (3)

Nithin Samuel
Nithin Samuel

Reputation: 11

You can try and use plain javascript create a variable and use below code

"window.location.hostname"

Upvotes: 0

Pardeep Jain
Pardeep Jain

Reputation: 86790

You can get full Location object using the code, where you can find everything related to the router including hostname, URL, params etc -

constructor(@Inject(DOCUMENT) private document) {
   console.log(document.location);
}

The object contains info -

{
  "href": "http://localhost:4200/",
  "ancestorOrigins": {},
  "origin": "http://localhost:4200",
  "protocol": "http:",
  "host": "localhost:4200",
  "hostname": "localhost",
  "port": "4200",
  "pathname": "/",
  "search": "",
  "hash": ""
}

Alternate -

window.location.href

Also, router object from angular contains much information about router/navigation.

Upvotes: 13

balajivaishnav
balajivaishnav

Reputation: 2893

Give try on this below code

Create a Variable of Location Type

public location: Location
this.location['_platformStrategy']._platformLocation.location.host

Upvotes: 1

Related Questions