Angular 13 中的分页符无法与 ngFor 一起使用

发布于 2025-01-15 22:12:19 字数 1021 浏览 0 评论 0原文

我已经做了一些解决方案的搜索,但 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;">&nbsp;</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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤还要旧人安 2025-01-22 22:12:19

我猜 css 语法有点偏离page-break-after:always

  <div style="page-break-after: always" *ngFor="let dataItem of allTheData" >
    <div><b>ID:</b> {{ dataItem.id }}</div>
  </div>

  <button (click)="onPrint()">Print</button>
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  name = 'Angular ' + VERSION.major;
  allTheData = [{ id: 1 }, { id: 2 }, { id: 3 }];
  constructor() {}

  onPrint() {
    window.print();
  }
}

工作示例: https://stackblitz.com /edit/angular-ivy-9wgfdt?file=src%2Fapp%2Fapp.component.ts

抱歉,我应该更清楚我的示例中有什么不同。我会改进我的评论。如果您应用了正确的 css 语法,则将 #testTemplate 移至

,例如。 ng-template 不会在模板中渲染,因此您没有任何可引用的内容。然后在ngAfterViewInit()中调用this.printer.printAngular(this.testTemplate);。带有 #testTemplate

在此周期中最早可用。
<ng-template let-allTheData>
  <div #testTemplate 
       style="page-break-after: always"
       *ngFor="let dataItem of allTheData">
    <div><b>ID:</b> {{ dataItem.id }}</div>
  </div>
ngAfterViewInit() {
   this.printer.printAngular(this.testTemplate);
}

I am guessing the css syntax is a bit off page-break-after: always.

  <div style="page-break-after: always" *ngFor="let dataItem of allTheData" >
    <div><b>ID:</b> {{ dataItem.id }}</div>
  </div>

  <button (click)="onPrint()">Print</button>
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  name = 'Angular ' + VERSION.major;
  allTheData = [{ id: 1 }, { id: 2 }, { id: 3 }];
  constructor() {}

  onPrint() {
    window.print();
  }
}

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 call this.printer.printAngular(this.testTemplate); in ngAfterViewInit(). <div> with #testTemplate becomes available earliest in this cycle.

<ng-template let-allTheData>
  <div #testTemplate 
       style="page-break-after: always"
       *ngFor="let dataItem of allTheData">
    <div><b>ID:</b> {{ dataItem.id }}</div>
  </div>
ngAfterViewInit() {
   this.printer.printAngular(this.testTemplate);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文