在AngularJS中调用$window.print()会导致打印预览显示空白页面。

4
我遇到了一个非常奇怪的问题,在AngularJS中调用$window.print时,我只能看到左上角的日期、顶部的页面名称、右下角的页码和左下角的网址。然而,页面上没有其他内容,这非常奇怪,因为普通的页面有很多内容。
我猜想这可能与页面上的ng-view有关,但我不太确定。我会添加打印样式表,但我的问题更多的是在于没有内容可以开始样式设计。有任何想法吗?

你使用的是哪个浏览器? - SK.
Chrome的最新版本 - Daniel Jamrozik
2个回答

1

尝试使用(不要忘记注入服务 $timeout):

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

我认为你的问题在于angular还没有完成digest循环并更新DOM。


1
使用此代码打印特定的
内容。
$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();
} 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接