Angular Get Current Used Port Number Example
Hello,
In this quick example, let's see angular get current used port number. let’s discuss about how to get port number in angular. I’m going to show you about angular get current running port. I would like to show you angular get port number using DOCUMENT. Alright, let’s dive into the steps.
In this example, I will show you how to get a port number in an angular application. we will use Inject, Injectable and DOCUMENT to get document.port 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 {
port = '';
/*------------------------------------------
--------------------------------------------
constructor
--------------------------------------------
--------------------------------------------*/
constructor(@Inject(DOCUMENT) private document: any) { }
/*------------------------------------------
--------------------------------------------
ngOnInit
--------------------------------------------
--------------------------------------------*/
ngOnInit() {
this.port = this.document.location.port;
console.log(this.port);
}
}
Output:
4200
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 Call Function Every X Seconds Example
- How to Use Custom Svg Icons in Angular Material?
- Angular 13 RxJS Observable with Httpclient Example
- Angular 13 Routing Module Example Tutorial
- Angular 13 HttpClient & Http Services Tutorial Example
- Angular Material Select Dropdown with Image Example
- Angular Image Upload with Progress Bar Example
- How to Generate Random String in Angular?
- Angular Slick Carousel/Slider Example
- Angular Pie Chart Example Tutorial
- Angular Material Datepicker Disable Specific Dates Example