Daniel Jamrozik
Daniel Jamrozik

Reputation: 1238

Calling $window.print() in angularjs causes the print preview to show a blank page

I have a very strange problem where, when I call $window.print in angularjs, all I see is the date on the top left, page name on the top, page number on the bottom right, and the url on the bottom left. However, there is no other content on the page, which is very weird since the regular page has plenty of content.

I guess of mine is that this has to do with an ng-view I have on the page, but I'm not too sure. I would add a print style sheet, but my problem is more on the side that there is no content to style to begin with. Any ideas?

Upvotes: 4

Views: 7127

Answers (2)

SK.
SK.

Reputation: 4358

Use this to print a particular div content.

$scope.printDiv = function(divName) {
  var printContents = document.getElementById(divName).innerHTML;
  var popupWin = window.open('', '_blank', 'width=300,height=300');
  popupWin.document.open()
  popupWin.document.write('<html><head><link rel="stylesheet" type="text/css" href="style.css" /></head><body onload="window.print()">' + printContents + '</html>');
  popupWin.document.close();
} 

Upvotes: 1

Mitra
Mitra

Reputation: 38

Try to use(don't forget to inject service $timeout):

$timeout(function() {
    $window.print();
})

I think your problem in that angular haven't finishes the digest cycle and updates the DOM.

Upvotes: 1

Related Questions