Angular Get Current Used Port Number Example


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 and angular 13 version.

Let’s see bellow example with output:


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

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


selector: 'app-root',

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

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


export class AppComponent implements OnInit {

port = '';






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






ngOnInit() {

this.port = this.document.location.port;






I hope it can help you…

Leave a Comment