Javascript 如何使用document.write.2

Javascript 如何使用document.write.2,javascript,angular,Javascript,Angular,在我的普通HTML页面中,我有一个简单的4行代码,如下所示: <script> var getData = JSON.parse(localStorage.getItem("templateType")); document.write(getData.template_code); // in angular its wriiten as getData["template_code"] console.log(getData.template_code); document.ge

在我的普通HTML页面中,我有一个简单的4行代码,如下所示:

<script>
var getData = JSON.parse(localStorage.getItem("templateType"));
document.write(getData.template_code); // in angular its wriiten as getData["template_code"]
console.log(getData.template_code);
document.getElementById("main-wrapper").innerHTML = getData.template_code;
</script>

var getData=JSON.parse(localStorage.getItem(“templateType”);
document.write(getData.template_code);//以getData[“template_code”]
console.log(getData.template_代码);
document.getElementById(“主包装”).innerHTML=getData.template\u代码;

如何在Angular 2中执行相同操作,我有一个组件
webview.component.html
webview.component.ts

您可以通过以下方式将文档对象导入Angular组件中:

import { Inject, Injectable } from '@angular/core';
import { DOCUMENT } from '@angular/platform-browser';
所以你用它:

@Injectable()
export class MyService {
  constructor(@Inject(DOCUMENT) private document: any) {
      document.write(getData.template_code);
  }
}

还有一个非常类似的问题。

您可以通过以下方式将文档对象导入角度组件中:

import { Inject, Injectable } from '@angular/core';
import { DOCUMENT } from '@angular/platform-browser';
所以你用它:

@Injectable()
export class MyService {
  constructor(@Inject(DOCUMENT) private document: any) {
      document.write(getData.template_code);
  }
}

还有一个非常类似的问题。

您可以在webview.component.htmlc中包含脚本块。您可以指导如何操作吗?举个例子,下面报告这是一个合理的副本:您可以在webview.component.htmlc中包含脚本块,您可以指导如何操作吗?下面有一个例子,这是一个合理的副本:这个解决方案看起来像是angular 1,我需要一个angular 1的解决方案2@user2828442然后用Angular 2的
Angular
标记你的问题,而不是
angularjs
@user2828442:这是一个Angular 2+解决方案。Angular 1没有批注,或
@Angular
包命名空间。此解决方案与Angular 1类似,我需要Angular 1的解决方案2@user2828442然后用Angular 2的
Angular
标记你的问题,而不是
angularjs
@user2828442:这是一个Angular 2+解决方案。Angular 1没有批注,也没有
@Angular
包命名空间。