1
我一直试图在修改html元素后打印,但元素尚未更改。 (Angular2) 此源代码是简化的。如何在修改元素后刷新html元素
<div *ngIf="displayType === 'screen'">
<div>This is Screen</div>
</div>
<div *ngIf="displayType === 'print'">
<div>This is Print</div>
</div>
而且当点击一个按钮时发生以下事件。
displayType: string = 'screen'; // default
OnPrint() {
this.displayType = 'print';
let tmp = document.createElement('div');
let el = this.elementRef.nativeElement.cloneNode(true);
tmp.appendChild(el);
let content = tmp.innerHTML;
let frame1 = document.createElement('iframe');
document.body.appendChild(frame1);
let frameDoc = frame1.contentWindow;
frameDoc.document.open();
frameDoc.document.write('<html><body>'+content+'</body></html>');
frameDoc.document.close();
setTimeout(function() {
window.frames["frame1"].focus();
window.frames["frame1"].print();
document.body.removeChild(frame1);
}, 500);
}
但内容是修改前的元素。 如何刷新元素?
这是什么目的。我无法理解这段代码。 Angular2不关心你动态添加的HTML。究竟应该更新什么? –
我需要在打印时更改布局。这就是为什么我需要修改html。我提到了Angular2,以防人们不知道Angular2 – user3289230