angular - 使用 ngxPrint 在隐藏的 div 中打印内容时无法读取 null 的属性“getElementsByTagName”
问题描述
- 我的html
<div *ngIf="isShown" id="print-section">
<!--Your html stuff that you want to print-->
</div>
<button printTitle="MyTitle" printSectionId="print-section" ngxPrint>print</button>
- 我的.ts
ngOnInit() {
this.isShown = false;
}
isShown
如果我设置为false,为什么不能打印div内容。
解决方案
*ngIf
是指令的语法糖[ngIf]
。所以你的模板实际上会扩展到
<ng-template [ngIf]="isShown">
<div id="print-section">
<!--Your html stuff that you want to print-->
</div>
</ng-template>
这ng-template
是更通用的 HTMLtemplate
标记的 Angular 特定实现。因此,当您看到条件为假时,它div
并没有像您说的那样隐藏,而是尚未呈现。因此,当您尝试时getElementsByTagName
,该元素尚不可用。
作为一种解决方法,您可以使用 CSS 来隐藏元素,而不是*ngIf
. 尝试以下
<div [style.display]="isShown ? 'block' : 'none'" id="print-section">
<!--Your html stuff that you want to print-->
</div>
推荐阅读
- javascript - JavaScript 仅在按下 Ctrl + F5 时有效
- python - 在布局中找不到 ID > 当我想点击提交按钮时
- git - 我需要在 git status 中取消看到字符
- c# - 对 Inspector 分配变量的未分配引用
- ios - 如果在 UITableViewCell 中隐藏,如何将 UIView 的大小缩小为零?
- masstransit - 使用快递重试消息
- javascript - 如何在不手动输入数据映射框的情况下从 JSON 文件中检索数据
- django - 我如何在管理员之外复制 admin.TabularInline(在用户端?)
- python - 是否有在机器人关闭之前运行的功能
- javascript - 如何在反应中使用搜索?