-
-
Save icu0755/9b0df132d7e2d57998a4e5d13ad59f35 to your computer and use it in GitHub Desktop.
A working angularJS print directive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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