我只想打印一次 div 。其次,它不应该打印。
$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 + '</body></html>');
popupWin.document.close();
}
<div>
<div>
Do not print
</div>
<div id="printable">
Print this div
</div>
<button ng-click="printDiv('printableArea');">Print Div</button>
</div>
总打印次数为 1。然后我想限制它不打印。我怎样才能在 Angular js中做到这一点。谁能帮我解决这个问题。
最佳答案
您可以在点击后添加标记
var clicked = false;
$scope.printDiv = function(divName) {
if (clicked) return;
clicked = true;
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 + '</body></html>');
popupWin.document.close();
}
关于javascript - 在 AngularJS 中只打印一次 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49728489/