Print a div using javascript in angularJS single page application
$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();
}
Print div with external css using angular js
You can access particular DOM element by using angular.element.find
angular.element.find('test');
console.log(t.html().text())
similar way you can set html of selected DOM to body
and print it as per your requirement.
print the content of a HTML DIV
Here is the answer I found
HTML and JS CODE
<div id="printable">
Print this div
</div>
<button ng-click="printDiv('printableArea');">Print Div</button>
$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();
}
I found it from here
AngularJS Print Hidden Div
You just need to add a style
tag with:
.ng-hide { display: none !important; }
Full code: (This work will not work in the snippet. To see the effect go the bin)
angular.module('app', []).controller('ctrl', function(){
});
function printDiv() { var popupWin = window.open('', '_blank', 'width=1000,height=600');
popupWin.document.write('<html><head><link href="public/css/style.css" ' + 'rel="stylesheet"><style>.ng-hide { display: none !important; }</style></head><body>' + document.querySelector('#div').innerHTML + '</html>');
setTimeout(function(){ popupWin.print(); popupWin.close(); }, 500);}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl" id="div"> Visible <div ng-hide="true">Text</div></div>
<button onclick="printDiv()">Print</button>
AngularJs: Display HTML elements from string variable in JSP page
Use AngularJs ng-bind-html
directive to render HTML.
( Docs )
<div class="panel-body" ng-bind-html="myvar"></div>
Print the contents of a DIV
Slight changes over earlier version - tested on CHROME
function PrintElem(elem)
{
var mywindow = window.open('', 'PRINT', 'height=400,width=600');
mywindow.document.write('<html><head><title>' + document.title + '</title>');
mywindow.document.write('</head><body >');
mywindow.document.write('<h1>' + document.title + '</h1>');
mywindow.document.write(document.getElementById(elem).innerHTML);
mywindow.document.write('</body></html>');
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10*/
mywindow.print();
mywindow.close();
return true;
}
Related Topics
How to Send Variables from One File to Another in JavaScript
Enable/Disable Submit Button If Checkbox Is Checked/Unchecked
Avoid Window Jump to Top When Clicking #-Links
Shorthand Http:// as // for Script and Link Tags? Anyone See/Use This Before
Value Was Evaluated Just Now with Console.Log on JavaScript Object
Can JavaScript Access Iframe Elements from the Parent Page
Set Cursor at a Length of 14 Onfocus of a Textbox
Get a List of All Folders in Directory
HTML Alignment Issue in One MAChine Only (Both IE8)
Uploading Image to Amazon S3 with HTML, JavaScript & Jquery with Ajax Request (No PHP)
What Does Angular 2 Hashtags in Template Mean
How to Resize Twitter Bootstrap Modal Dynamically Based on the Content
CSS Delivery Optimization: How to Defer CSS Loading
Detect If Browser Is Running on an Android or iOS Device
Best JavaScript Solution for Client-Side Form Validation and Interaction