我想在 showlist.component.html 中添加一些文字,我的 showlist.component.ts 代码如下,我不知道我怎么能用角度为document.body
2.请指导
import { Component, OnInit } from '@angular/core';
import { DOCUMENT } from '@angular/platform-browser';
@Component({
selector: 'app-showlist',
templateUrl: './showlist.component.html',
styleUrls: ['./showlist.component.css']
})
export class ShowlistComponent implements OnInit {
public myname = "saurabh";
constructor() { }
ngOnInit() {
//this.myname.appendTo(document.body);
document.body(this.myname);
//document.write(this.myname);
}
}
答案 0 :(得分:16)
您可以执行以下操作来访问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: any) {}
ngOnInit() {
this.document.body.innerHTML = this.myname;
}
}