Skip to content

Instantly share code, notes, and snippets.

@icu0755
Forked from agray/printDirective.js
Created October 21, 2016 14:01
Show Gist options
  • Save icu0755/9b0df132d7e2d57998a4e5d13ad59f35 to your computer and use it in GitHub Desktop.
Save icu0755/9b0df132d7e2d57998a4e5d13ad59f35 to your computer and use it in GitHub Desktop.
A working angularJS print directive
(function (angular) {
"use strict";
function printDirective() {
var printSection = document.getElementById("printSection");
function printElement(elem) {
// clones the element you want to print
var domClone = elem.cloneNode(true);
if (!printSection) {
printSection = document.createElement("div");
printSection.id = "printSection";
document.body.appendChild(printSection);
} else {
printSection.innerHTML = "";
}
printSection.appendChild(domClone);
}
function link(scope, element, attrs) {
element.on("click", function () {
var elemToPrint = document.getElementById(attrs.printElementId);
if (elemToPrint) {
printElement(elemToPrint);
window.print();
}
});
}
return {
link: link,
restrict: "A"
};
}
angular.module("app.directives").directive("ngPrint", [printDirective]);
}(window.angular));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment