Javascript frames[";printElem";].print()提供空预览

Javascript frames[";printElem";].print()提供空预览,javascript,angularjs,iframe,Javascript,Angularjs,Iframe,我使用的angularjs指令 function printDiv() { return { restrict: 'A', link: function(scope, element, attrs) { element.bind('click', function(evt) { evt.preventDefault(); PrintElem(attrs.printDiv

我使用的angularjs指令

function printDiv() {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            element.bind('click', function(evt) {
                evt.preventDefault();
                PrintElem(attrs.printDiv);
            });

            function PrintElem(elem) {
                PrintWithIframe($(elem).html());
            }

            function PrintWithIframe(data) {
                if ($('iframe#printf').size() == 0) {
                    $('html').append('<iframe id="printf" name="printf"></iframe>');     
                    var mywindow = window.frames["printf"];
                    mywindow.document.write('<html><head><link href="css/bootstrap.min.css" rel="stylesheet" type="text/css"><link href="css/style.css" rel="stylesheet" type="text/css"></head><body>'
                        + data +
                        '</body></html>');

                    $(mywindow.document).ready(function() {
                        mywindow.print();

                        // $('iframe#printf').remove();
                    });
                } else {
                    window.frames["printf"].print();
                }

                return true;
            }
        }
    };
}
但是我发现
print()
内部的
setTimeout
从未被调用。 你知道如何让打印在第一时间工作吗?谢谢

更新:在我将
document.write
替换为
document.body.innerHTML
后,预览不再为空,但外部css样式从未应用于预览,尽管我可以在
iframe
中看到样式是正确的。我在Firefox和Chrome上都试过了

$('html').append('<iframe id="printf" name="printf"></iframe>');

window.frames["printf"].document.body.innerHTML='<link href="css/bootstrap.min.css" rel="stylesheet" type="text/css"><link href="css/style.css" rel="stylesheet" type="text/css">' + $(attrs.printDiv).html();
$(window.frames["printf"].document).ready(function() {
    window.frames["printf"].window.focus();
    window.frames["printf"].window.print();

    // $('iframe#printf').remove();
});
$('html')。追加(“”);
window.frames[“printf”].document.body.innerHTML='+$(attrs.printDiv.html();
$(window.frames[“printf”].document).ready(函数(){
window.frames[“printf”].window.focus();
window.frames[“printf”].window.print();
//$('iframe#printf')。删除();
});

框架应该在主体内部,
应该在“@charlietfl”中这与这里的问题无关。这并不是说这是您需要的修复,但至少应该使用有效的html
$('html').append('<iframe id="printf" name="printf"></iframe>');

window.frames["printf"].document.body.innerHTML='<link href="css/bootstrap.min.css" rel="stylesheet" type="text/css"><link href="css/style.css" rel="stylesheet" type="text/css">' + $(attrs.printDiv).html();
$(window.frames["printf"].document).ready(function() {
    window.frames["printf"].window.focus();
    window.frames["printf"].window.print();

    // $('iframe#printf').remove();
});