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:
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();
}