【发布时间】:2017-11-01 10:30:44
【问题描述】:
我正在使用 angualr 2。
我正在尝试打印 html div。
这是我的代码
<div id="print-section">
// Html code
</div>
<button (click)="open()">print</button>
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(`
<html>
<head>
<title>Print tab</title>
</head>
<body onload="window.print();window.close()">${printContents}</body>
</html>`
);
popupWin.document.close();
}
这是我的 open()
open(){
//I called Api using service
let scope=this;
setTimeout(function() { scope.print(); }, 3000);
}
但我犯了这个错误
错误类型错误:无法读取 null 的属性“文档”。
我该如何解决这个问题?
【问题讨论】:
-
popupWin.documentElement.open也许? -
感谢您的回复。我仍然遇到同样的错误吗?
标签: javascript angular