在 Angular 组件中获取 <html> 标签

fun*_*kid 2 javascript typescript angular

<body>并且<head>可以通过注入在 Angular 组件中获取标签DOCUMENT,如下所示:

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
}
Run Code Online (Sandbox Code Playgroud)

但是可以<html>在 Angular 组件中获取标签吗?

Rea*_*lar 8

引用将在浏览器中的根documentElement元素。<html>

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);
   }
}
Run Code Online (Sandbox Code Playgroud)