- My html
<div *ngIf="isShown" id="print-section">
<!--Your html stuff that you want to print-->
</div>
<button printTitle="MyTitle" printSectionId="print-section" ngxPrint>print</button>
- My .ts
ngOnInit() {
this.isShown = false;
}
Why can't the div content be printed if I set isShown to false.

The
*ngIfis a syntactic sugar for the directive[ngIf]. So your template would actually be expanded toThe
ng-templateis Angular specific implementation of more generic HTMLtemplatetag. So as you see when the condition is false, thedivisn't hidden like you say but rather not rendered yet. So when you attemptgetElementsByTagName, the element isn't available yet.As a workaround, you could use CSS to hide the element instead of
*ngIf. Try the following