我正在尝试创建一个按钮,用户可以在其中打印页面以获取其记录,但它仍然使用形式显示空白,我假设因为表单是动态生成的。我在“打印”按钮中使用了print指令:
(function (angular) {
'use strict';
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.appendChild(domClone);
window.print();
}
return {
link: link,
restrict: 'A'
};
}
angular.module('formlyApp').directive('ngPrint', [printDirective]);
}(window.angular));
这是打印按钮:
<button class="btn btnSubmit" type="submit" ng-print print-element-id="printSection" ng-hide="vm.iForm.$invalid">Print and Save</button>
当我按下按钮时弹出打印窗口,但除了显示页面名称和日期之外,页面是空白的。如何打印出格式化的字段?