Print HTML DIV Content
I want to print the contents of an html div, and I'm currently using this code.
<div id="content">
</div>
var printContents = document.getElementById("content").innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
Printing is working correctly. But after that, my other functions stop responding. I am using angular.js.
any idea?
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 here
if you use angularjs use
<div ng-bind-html="content |trustAs">
</div>
in your controller
$scope.content='<div> Your html</div>';
app.filter('trustAs', ['$sce',
function($sce) {
return function (input, type) {
if (typeof input === "string") {
return $sce.trustAs(type || 'html', input);
}
console.log("trustAs filter. Error. input isn't a string");
return "";
};
}
]);
You can also use ng-sanitize to get the same result fooobar.com/questions/27112 / ...