Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get <html> tag in Angular component

<body> and <head> tag can be get in Angular component by injectingDOCUMENT, like this:

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

export class TestComponent {
  constructor(
    @Inject(DOCUMENT) private document: Document
  )

  // get <head>
  // this.document.head

  // get <body>
  // this.document.body
}

But is it possible to get <html> tag in Angular component?

like image 216
funkid Avatar asked Sep 01 '25 02:09

funkid


1 Answers

The documentElement references the root element which will be <html> in the browser.

https://developer.mozilla.org/en-US/docs/Web/API/Document/documentElement

@Component({..})
public class ExampleComponent {
   public constructor(@Inject(DOCUMENT) doc: Document) {
      console.log(doc.documentElement);
   }
}
like image 149
Reactgular Avatar answered Sep 02 '25 17:09

Reactgular