How to Get Domain Name in Angular App?
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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Angular 13 File Upload Tutorial Example
- Angular 13 Reactive Forms Validation Tutorial Example
- Angular Material Multi Select Dropdown Example
- Angular Material Datepicker Disable Previous Dates Example
- Angular Textarea Auto Height Example
- How to Get Element Height and Width in Angular?
- Angular Google Maps Marker Click Event Example
- Angular Material Copy to Clipboard Example
- Angular Material Selection List Example | Angular mat-selection-list
- Angular NgClass Conditional Example
- How to Remove A Component in Angular?
- How to Disable Browser Refresh Button in Angular?