如何使用 TS 在 Angular 4 中打印页面 [英] How to print a page in Angular 4 with TS

查看:34
本文介绍了如何使用 TS 在 Angular 4 中打印页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网络应用程序,而在某些页面中,我需要制作打印机来打印页面,但我有一个侧边栏,其余部分是组件的页面,怎么可能不只打印此组件的侧边栏,我已经在TS中使用过这段代码.

print() {窗口.打印();}

Html 代码从此开始.

div class="container">//这里我有所有的HTML源代码

解决方案

首先给那个组件分配一个id,然后:

const printContent = document.getElementById("componentID");const WindowPrt = window.open('', '', 'left=0,top=0,width=900,height=900,toolbar=0,scrollbars=0,status=0');WindowPrt.document.write(printContent.innerHTML);WindowPrt.document.close();WindowPrt.focus();WindowPrt.print();WindowPrt.close();

I have an web app while in some pages I need to make a printer to print the page but I have a side bar and the rest ist the page for a component, how it is possible to not print the sidebar only this component, I have used in TS this code.

print() {
window.print();
}

Html code starts from this one.

div class="container">
//Here I have all the HTML source

</div>

解决方案

first of all assign an id to that component, then:

const printContent = document.getElementById("componentID");
const WindowPrt = window.open('', '', 'left=0,top=0,width=900,height=900,toolbar=0,scrollbars=0,status=0');
WindowPrt.document.write(printContent.innerHTML);
WindowPrt.document.close();
WindowPrt.focus();
WindowPrt.print();
WindowPrt.close();

这篇关于如何使用 TS 在 Angular 4 中打印页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆