How To Inject Document Object In Angular Service

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?

2 thoughts on “How To Inject Document Object In Angular Service”

Leave a Reply

Your email address will not be published. Required fields are marked *