Injecting the document object in an Angular component is quite straightforward. All you need to do is import the DOCUMENT DI token from @angular/common.
- import { DOCUMENT } from '@angular/common';
- import { Component, Inject } from '@angular/core';
- import { DOCUMENT } from '@angular/common';
- @Component({.....})
- export class OrderComponent {
- constructor (
- @Inject(DOCUMENT) private document: Document
- ) {}
- }
- import { Inject, Injectable } from '@angular/core';
- import { DOCUMENT } from '@angular/common';
- @Injectable({.....})
- export class OrderService {
- constructor (
- @Inject(DOCUMENT) private document: Document
- ) {}
- }

Join the conversation! Your thoughts help the community grow.