0

In my normal HTML page I am having a simple 4 line code shown below:

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

How can I do the same in Angular 2, I have a component webview.component.html and webview.component.ts?

halfer
  • 19,824
  • 17
  • 99
  • 186
user2828442
  • 2,415
  • 7
  • 57
  • 105

1 Answers1

2

You can import document object inside your angular component in this way:

import { Inject, Injectable } from '@angular/core';
import { DOCUMENT } from '@angular/platform-browser';

So you use it:

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

There is a pretty similar question here.

Luillyfe
  • 6,183
  • 8
  • 36
  • 46