Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Angular 2中注入document.body_Javascript_Angular - Fatal编程技术网

Javascript 如何在Angular 2中注入document.body

Javascript 如何在Angular 2中注入document.body,javascript,angular,Javascript,Angular,我想在showlist.component.html中添加一些文本。下面给出了我的代码,我不知道如何在Angular 2中使用document.body import { Component, OnInit } from '@angular/core'; import { DOCUMENT } from '@angular/platform-browser'; @Component({ selector: 'app-showlist', templateUrl: './showlist

我想在showlist.component.html中添加一些文本。下面给出了我的代码,我不知道如何在Angular 2中使用
document.body

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);
  }

}

要访问
文档.正文

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;
  }
}

你可以像那样使用它
document.body
返回
HTML元素。在上面的例子中,
document.write(this.myname)
应将所有正文内容替换为
saurabh
。如果你想引用一个特定的元素,请勾选这个是的,当然这是一种高级的东西,但我会尽我所能,
DOCUMENT
是依赖注入(aka DI)标记,你的组件依赖它来引用文档和非类依赖(因为它不是类)我们需要
@Inject
decorator
this.document.body.innerHTML=this.myname
,如果我想将
this.myname
与其他页面数据一起添加,那么我需要做什么?上面的一行只显示了
这一点。我的名字
在页面上,并删除了
@angular/platform browser
中的所有其他
文档
已被弃用,改为
@angular/common
仅供参考:我必须使用我的IDE自动导入:结果如下:从导入{DOCUMENT}“@angular/platform browser/src/dom/dom_tokens”;