Print a div using javascript in angularJS single page application

前端 未结 7 966
野性不改
野性不改 2020-11-28 02:44

I have a single page web application using angularJS. I need to print a div of certain page. I tried the following:

The page contains few div (print.html)

         


        
相关标签:
7条回答
  • 2020-11-28 03:40

    Two conditional functions are needed: one for Google Chrome, and a second for the remaining browsers.

    $scope.printDiv = function (divName) {
    
        var printContents = document.getElementById(divName).innerHTML; 
    
        if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
            var popupWin = window.open('', '_blank', 'width=600,height=600,scrollbars=no,menubar=no,toolbar=no,location=no,status=no,titlebar=no');
            popupWin.window.focus();
            popupWin.document.write('<!DOCTYPE html><html><head>' +
                '<link rel="stylesheet" type="text/css" href="style.css" />' +
                '</head><body onload="window.print()"><div class="reward-body">' + printContents + '</div></body></html>');
            popupWin.onbeforeunload = function (event) {
                popupWin.close();
                return '.\n';
            };
            popupWin.onabort = function (event) {
                popupWin.document.close();
                popupWin.close();
            }
        } else {
            var popupWin = window.open('', '_blank', 'width=800,height=600');
            popupWin.document.open();
            popupWin.document.write('<html><head><link rel="stylesheet" type="text/css" href="style.css" /></head><body onload="window.print()">' + printContents + '</body></html>');
            popupWin.document.close();
        }
        popupWin.document.close();
    
        return true;
    }
    
    0 讨论(0)
提交回复
热议问题