ItSolutionStuff.com

Angular - Cannot find name 'DOCUMENT' - Solved

By Hardik Savani β€’ May 2, 2024
Angular

This tutorial shows you Cannot find name 'DOCUMENT' in angular. you will learn angular error ts2304 cannot find name 'DOCUMENT'. you will learn angular cannot find document error. let’s discuss about angular cannot find name document.

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

In this post, I will give you the solution of "Cannot find name 'DOCUMENT'" in angular application. we need to import DOCUMENT from @angular/common library. so let's see below the solution and full code.

Solution:

let's import DOCUMENT from @angular/common npm package, as bellow:

Import Output:

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

Let's see bellow working code:

Example:

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);

}

}

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 Material Checkbox Change Color Example

Read Now β†’
β˜…

Angular Fullcalendar Example Tutorial

Read Now β†’
β˜…

Angular 13 Stripe Payment Integration Example

Read Now β†’
β˜…

Angular 13 Login with Google / Gmail Account Tutorial

Read Now β†’
β˜…

Angular 13 RxJS Observable with Httpclient Example

Read Now β†’
β˜…

Angular 13 Multiple Image Upload with Preview Tutorial

Read Now β†’
β˜…

Angular 13 Reactive Forms Validation Tutorial Example

Read Now β†’
β˜…

Angular Image Upload with Progress Bar Example

Read Now β†’
β˜…

How to Remove Hash from URL in Angular?

Read Now β†’
β˜…

Angular Bubble Chart Example Tutorial

Read Now β†’
β˜…

Angular Google Maps using agm/core Example

Read Now β†’