Created
February 27, 2015 03:42
-
-
Save agray/6f8e0325f66be2be808e 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)); |
Thanks for sharing this but i have issue i am getting extra one blank page
is there any solution for that ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This will work on any div it is attached to as you would expect.
The version here, just doesn't target divs correctly.
This directive can be used in an MVC Razor file like so: