title: Cannot read property 'document' of null when try to print html document
body: <p>I am using angualr 2.</p>
  
  <p>I am trying to print html div.</p>
  
  <p>here is my code </p>
  
  <pre><code>&lt;div id="print-section"&gt;
    // Html code
  &lt;/div&gt;
  &lt;button (click)="open()"&gt;print&lt;/button&gt;
  
   print(): void {
      let printContents, popupWin;
      printContents = document.getElementById('print-section').innerHTML;
      popupWin = window.open('', '_blank', 'top=0,left=0,height=100%,width=auto');
      popupWin.document.open();
      popupWin.document.write(`
        &lt;html&gt;
          &lt;head&gt;
            &lt;title&gt;Print tab&lt;/title&gt;
           &lt;/head&gt;
      &lt;body onload="window.print();window.close()"&gt;${printContents}&lt;/body&gt;
        &lt;/html&gt;`
      );
      popupWin.document.close();
  }
  </code></pre>
  
  <p>here is my open()</p>
  
  <pre><code>open(){
    //I called Api using service
     let scope=this;
     setTimeout(function() { scope.print(); }, 3000);
  }
  </code></pre>
  
  <p>But i am getting this err </p>
  
  <blockquote>
    <p>ERROR TypeError: Cannot read property 'document' of null.</p>
  </blockquote>
  
  <p>How can i fix this issue ?</p>