Window.print()

时间:2016-02-04 17:45:13

标签: javascript angularjs dom printing

我们正在为客户建立一个SPA,我们已经与AngularJS合作了好几年了(还没有迁移到2.0):)我们只在webkit浏览器上才有这个有趣的事情。

以下是我们的一个模块及其指令:

(function () {
    angular
        .module('app.medical.reports.referrals.directives', [])
        .config(config)
        .run(run)
        .directive('medicalReferralsReportsDirector', medicalReferralsReportsDirector);

    function config() {
    }

    function run() {
    }

    function medicalReferralsReportsDirector($window, $compile, $timeout, medicalReportsService) {
        var directive = {};

        directive.restrict = "EA";
        directive.scope = true;
        directive.link = linkFn;
        directive.template = ['<div ng-include="print.printObject.templateUrl"></div>'].join('');

        return directive;

        function linkFn(scope, element, attrs) {
            scope.$on('$includeContentLoaded', function (e, data) {
                var bindToThisElement = angular.element(angular.element(element.children()[0]).children()[0]),
                    bindingFunction = $compile(bindToThisElement);

                bindingFunction(scope);

                $timeout(function () {

                    //TODO Strasen fix za webkit - STRASEN
                    $timeout(function () {
                        $window.print();
                    });
                    scope.print.printObject.startPrintRendering = false;
                    scope.$destroy();
                });
            });
        }
    }
})();

我们有其他服务侦听一个事件,该事件告诉有事要打印,然后我们将这个指令(作为属性)附加到打印容器的父DOM元素,然后我们发出请求(如果不是缓存)用于html模板(在scope属性中传递的url - &#34; print.printObject.templateUrl&#34;)。 然后在&#39; $ includeContentLoaded&#39;上,我们只找到DOM元素并在范围上调用$ compile。

一切都很好,但有一件非常有趣的事情正在发生。仅在webkit浏览器上,如果我们不将$ window.print()函数放在$ timeout函数中,它会在应该打印的内容之后插入一个空白页面。

在Mozilla或Opera中不会发生这种情况。

请问,这个问题不应该与这个问题混淆 - Calling $window.print() in angularjs causes the print preview to show a blank page 因为我们已经找到问题的解决方案/黑客,我们只是要求解释是否有人可以提供。

非常感谢你提前...

另外: 我差点忘了提到那个,    在打印完成并删除打印窗口后,我们销毁范围,从而删除所有DOM元素上的指令的所有引用,所以如果你只需按(Ctrl + P)或只是通过命令行调用 - window.print( ),在打印完成并删除指令后,空白页将消失,一切似乎都很好。

我们尝试了很多东西,如果没有$ timeout功能,我们就无法工作。我们的猜测是,在$ compile函数中发生内部事务,因为如果我们不使用这个指令(因此不要使用$ compile)并且只是在DOM中放入一些静态内容来打印那一切都很好:)。奇怪至少...

很抱歉这篇长篇文章,我没有马铃薯:)),但我会感谢那些试图给出答案的人:)

0 个答案:

没有答案
相关问题