How to inject document.body in Angular 2

前端 未结 1 1460
谎友^
谎友^ 2021-02-19 02:54

I want to add some text in showlist.component.html. My code is given below, I am not aware how to use document.body in Angular 2.

i         


        
相关标签:
1条回答
  • you do the following to access the document.body

    import { Component, OnInit, Inject } from '@angular/core';
    import { DOCUMENT } from '@angular/common';
    
    @Component({
      selector: 'app-showlist',
      templateUrl: './showlist.component.html',
      styleUrls: ['./showlist.component.css']
    })
    export class ShowlistComponent implements OnInit {
      public myname = "saurabh";
    
      constructor(@Inject(DOCUMENT) private document: Document) {}
    
      ngOnInit() {
        this.document.body.innerHTML = this.myname;
      }
    }
    
    0 讨论(0)
提交回复
热议问题