Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Open print dialog on page load in Angular 17

Having this script tag in any HTML page would have open the print dialog box on page load but in Angular 17, this do not get rendered.

<script>window.onload = function(){ window.print(); };</script>
<div>This is a print page</div>

I tried adding this JS code too in the .ts file but it throws and error:

export class PrintComponent implements OnInit {
  constructor() { }
  ngOnInit(): void {
    onload='window.print()';
  }
}

ERROR:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

print.component.ts:13:5 - error TS2322: Type '"window.print()"' is not assignable to type '((this: Window, ev: Event) => any) | null'.

13     onload='window.print()';

How do I call window.print on page load in an Angular 17 project? I have searched far and wide without any solution.

>Solution :

if you have ssr enabled project:

constructor() {
afterNextRender(() => {
  window.print();
})

}

if you have browser only project:

ngOnInit(): void {
  window.print();
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading