ItSolutionStuff.com

How to Get Domain Name in Angular App?

By Hardik Savani • May 2, 2024
Angular

Hi,

Are you looking for example of angular get domain name. if you want to see example of how to get domain name in angular then you are a right place. Here you will learn angular universal get domain name. I’m going to show you about angular get domain name using DOCUMENT. Let's see bellow example angular get document location hostname.

In this example, I will show you how to get a domain name in an angular application. we will use Inject, Injectable and DOCUMENT to get document.location details. so let's see below ts file which you can use in your service file as well.

You can use this example with angular 8, angular 9, angular 10, angular 11, angular 12, angular 13, angular 14, angular 15, angular 16 and angular 17 version.

Let's see bellow example with output:

src/app/app.component.s

import { Component, OnInit, Inject, Injectable } from '@angular/core';

import { DOCUMENT } from '@angular/common';

@Component({

selector: 'app-root',

templateUrl: './app.component.html',

styleUrls: ['./app.component.css']

})

export class AppComponent implements OnInit {

domain = '';

/*------------------------------------------

--------------------------------------------

constructor

--------------------------------------------

--------------------------------------------*/

constructor(@Inject(DOCUMENT) private document: any) { }

/*------------------------------------------

--------------------------------------------

ngOnInit

--------------------------------------------

--------------------------------------------*/

ngOnInit() {

this.domain = this.document.location.hostname;

console.log(this.domain);

}

}

Output:

localhost

I hope it can help you...

Tags: Angular
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

šŸ“ŗ Subscribe on YouTube

We Are Recommending You

ā˜…

Angular 13 File Upload Tutorial Example

Read Now →
ā˜…

Angular 13 Reactive Forms Validation Tutorial Example

Read Now →
ā˜…

Angular Material Multi Select Dropdown Example

Read Now →
ā˜…

Angular Material Datepicker Disable Previous Dates Example

Read Now →
ā˜…

Angular Textarea Auto Height Example

Read Now →
ā˜…

How to Get Element Height and Width in Angular?

Read Now →
ā˜…

Angular Google Maps Marker Click Event Example

Read Now →
ā˜…

Angular Material Copy to Clipboard Example

Read Now →
ā˜…

Angular Material Selection List Example | Angular mat-selection-list

Read Now →
ā˜…

Angular NgClass Conditional Example

Read Now →
ā˜…

How to Remove A Component in Angular?

Read Now →
ā˜…

How to Disable Browser Refresh Button in Angular?

Read Now →