Categories
Angular Answers

How to print HTML template in Angular and TypeScript?

Spread the love

Sometimes, we want to print HTML template in Angular and TypeScript.

In this article, we’ll look at how to print HTML template in Angular and TypeScript.

How to print HTML template in Angular and TypeScript?

To print HTML template in Angular and TypeScript, we call the window.print method.

For instance, we write

<div class="doNotPrint">Header is here.</div>

<div>hello world</div>

<div class="doNotPrint">
  my footer is here.
  <button (click)="onPrint()">Print</button>
</div>

to add some content in the template.

Then we write

//...
export class AppComponent {
  //...
  onPrint() {
    window.print();
  }
}

to add the onPrint method in our component.

It calls window.print to open the print dialog.

Then we write

@media print {
  .doNotPrint {
    display: none !important;
  }
}

to hide the stuff with the doNotPrint class in the printout.

Conclusion

To print HTML template in Angular and TypeScript, we call the window.print method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *