- import Inject from @angular/core and DOCUMENT from @angular/common
import { Component, Inject, ... } from '@angular/core';
import { DOCUMENT } from '@angular/common';
- Inject the DOCUMENT interface inside your class' constructor like this:
export class MyComponent {
// ...
constructor(@Inject(DOCUMENT) private document: Document) {}
- Now you can use the document object inside your class methods. Intellisense will display all the methods and properties of the Document interface, such as querySelector, getElementById, addEventListener etc..
myMethod() {
this.document.<whatever ...>
// ...
}
