Why should we bother using import { DOCUMENT } from '@angular/common'
?
Even without it I can access the document module
document.getElementById('TestID).focus()
The only difference I see with the import is having it attached to this
keyword.
this._document.getElementById('TestID').focus();
Is there any real benefit to importing DOCUMENT from Angular's common module?
I did some research but cannot see any information on this.
In our use case, the most important reason we inject the DOCUMENT
instead of using it directly is for Server Side Rendering. If you call document.xxx
directly in any regular lifecycle hooks like ngOnInit
/ngAfterViewInit
, you will mostly probably get document not defined
error during SSR. Using DI to inject it could avoid this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With