forked from gilf/ngPrint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngPrint.js
45 lines (37 loc) · 1.31 KB
/
ngPrint.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
(function (angular) {
'use strict';
var mod = angular.module('ngPrint', []);
function printDirective() {
var printSection = document.getElementById('printSection');
// if there is no printing section, create one
if (!printSection) {
printSection = document.createElement('div');
printSection.id = 'printSection';
document.body.appendChild(printSection);
}
function link(scope, element, attrs) {
element.on('click', function () {
var elemToPrint = document.getElementById(attrs.printElementId);
if (elemToPrint) {
printElement(elemToPrint);
}
});
window.onafterprint = function () {
// clean the print section before adding new content
printSection.innerHTML = '';
};
}
function printElement(elem) {
// clones the element you want to print
var domClone = elem.cloneNode(true);
printSection.innerHTML = '';
printSection.appendChild(domClone);
window.print();
}
return {
link: link,
restrict: 'A'
};
}
mod.directive('ngPrint', [printDirective]);
}(window.angular));