最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get <html> tag in Angular component - Stack Overflow

programmeradmin3浏览0评论

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

import { DOCUMENT } from '@angular/mon';
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 ponent?

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

import { DOCUMENT } from '@angular/mon';
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 ponent?

Share Improve this question asked Dec 28, 2019 at 11:32 funkidfunkid 6752 gold badges12 silver badges31 bronze badges 3
  • 3 And why exactly do you want that? – SiddAjmera Commented Dec 28, 2019 at 11:35
  • 2 document.documentElement? – evolutionxbox Commented Dec 28, 2019 at 11:35
  • @SiddAjmera sometimes, class or style may be used on <html>. But add these attributes directly on <html> in the root index.html will take effect globally. Set encapsulation: ViewEncapsulation.None in specific ponent to ignore the shadow dom maybe a promise way, but it will affect all the "none encapusulation" ponents, which I don't want either. – funkid Commented Dec 28, 2019 at 11:44
Add a ment  | 

1 Answer 1

Reset to default 10

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

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

@Component({..})
public class ExampleComponent {
   public constructor(@Inject(DOCUMENT) doc: Document) {
      console.log(doc.documentElement);
   }
}
发布评论

评论列表(0)

  1. 暂无评论