How To Inject Document Object In Angular Service

In this short post, we’re going to understand how we can inject document object within an Angular service.

Angular comes bundled with a DI token that refers to the document object. We can use the @Inject decorator to inject this token within our service or component.

import { Inject, Injectable } from '@angular/core';
import { DOCUMENT } from '@angular/common';

@Injectable()
export class MyCustomService {
  constructor(@Inject(DOCUMENT) private document: Document) {
  
  }
}

Must Read: Var, let and const- what’s the difference?