0

I want to print div only one time. Second it should not print.

$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>

The total print count is one. Then i want to restrict it to not print. How can I do this in angular js. Can anyone help me on this.

2 Answers 2

1

You can add flag when you already clicked

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();
} 
Sign up to request clarification or add additional context in comments.

Comments

1

You can have a variable for the count. You can configure totalCount option dynamically.

var count = {
    totalCount: 1,
    currentCount: 0
};
$scope.printDiv = function(divName) {
   if (count.totalCount == count.currentCount) {
       console.log("Already Printed");
       return;
   }
   count.totalCount += 1;
   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();
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.