ItSolutionStuff.com

Angular Get Current Used Port Number Example

By Hardik Savani β€’ May 2, 2024
Angular

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...

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 Call Function Every X Seconds Example

Read Now β†’
β˜…

How to Use Custom Svg Icons in Angular Material?

Read Now β†’
β˜…

Angular 13 RxJS Observable with Httpclient Example

Read Now β†’
β˜…

Angular 13 Routing Module Example Tutorial

Read Now β†’
β˜…

Angular 13 HttpClient & Http Services Tutorial Example

Read Now β†’
β˜…

Angular Material Select Dropdown with Image Example

Read Now β†’
β˜…

Angular Image Upload with Progress Bar Example

Read Now β†’
β˜…

How to Generate Random String in Angular?

Read Now β†’
β˜…

Angular Slick Carousel/Slider Example

Read Now β†’
β˜…

Angular Pie Chart Example Tutorial

Read Now β†’
β˜…

Angular Material Datepicker Disable Specific Dates Example

Read Now β†’