Angular 13 中的分页符无法与 ngFor 一起使用
我已经做了一些解决方案的搜索,但 Angular 13 组件中的分页符不会破坏页面。
我正在使用一个简单的 ng-template (为了可读性而删除了额外的字段):
<ng-template #testTemplate let-allTheData>
<div *ngFor="let dataItem of allTheData" >
<div><b>ID:</b> {{ dataItem.id }}</div>
<div style="break-after:always;"> </div> <-- non-working page break
</div>
该模板设置为 ViewChild,如下所示:
@ViewChild('testTemplate ') testTemplate !: TemplateRef<any>;
数据通过循环传入并处理
data.forEach(x => {
theThing = {};
theThing.id = arrtId;
this.theData.push(theThing);
});
并且使用此行调用打印对话框(使用 ngx-print ):
this.printer.printAngular(this.testTemplate);
数据在打印屏幕上显示正常,但模板中的分页符没有插入分页符 - 模板是否干扰?我还尝试过“break-after:page”、“break-after:auto”甚至“break-before:page”、“break-before:auto”和“break-before:always”。我还尝试使用我在一些搜索中看到的 CSS 媒体查询,并将“!Important”添加到内联 CSS 中。还没有什么效果。有人有什么想法吗?谢谢你!
I've done some searching around for a solution, but page breaks in an Angular 13 component will not break pages.
I am using a simple ng-template (extra fields removed for readability):
<ng-template #testTemplate let-allTheData>
<div *ngFor="let dataItem of allTheData" >
<div><b>ID:</b> {{ dataItem.id }}</div>
<div style="break-after:always;"> </div> <-- non-working page break
</div>
The template is setup as a ViewChild like this:
@ViewChild('testTemplate ') testTemplate !: TemplateRef<any>;
The data gets passed in and processed through a loop
data.forEach(x => {
theThing = {};
theThing.id = arrtId;
this.theData.push(theThing);
});
And the print dialog gets called with this line (using ngx-print):
this.printer.printAngular(this.testTemplate);
The data shows up fine on the print screen, but the page break in the template does not insert a page break - is the template interfering? I've also tried "break-after: page," "break-after: auto" and even "break-before: page," "break-before: auto" and "break-before: always." I also tried using the CSS media queries I've seen in a few searches and adding "!Important" to the inline CSS. Nothing has yet worked. Does anyone have any ideas? Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜 css 语法有点偏离
page-break-after:always
。工作示例: https://stackblitz.com /edit/angular-ivy-9wgfdt?file=src%2Fapp%2Fapp.component.ts
抱歉,我应该更清楚我的示例中有什么不同。我会改进我的评论。如果您应用了正确的 css 语法,则将
#testTemplate
从
移至,例如。
ng-template
不会在模板中渲染,因此您没有任何可引用的内容。然后在ngAfterViewInit()
中调用this.printer.printAngular(this.testTemplate);
。带有#testTemplate
的在此周期中最早可用。
I am guessing the css syntax is a bit off
page-break-after: always
.Working example: https://stackblitz.com/edit/angular-ivy-9wgfdt?file=src%2Fapp%2Fapp.component.ts
Sorry, I should have been clearer what's different in my example. I'll improve my comment. If you have applied the correct css syntax then move
#testTemplate
away from<ng-template>
to a<div>
, for example.ng-template
does not get rendered in template, so you have nothing to reference. Then callthis.printer.printAngular(this.testTemplate);
inngAfterViewInit()
.<div>
with#testTemplate
becomes available earliest in this cycle.