MOCKSTACKS
EN
Questions And Answers

More Tutorials









Angular Print

Print Service

Service:


angular.module('core').factory('print_service', ['$rootScope', '$compile', '$http',
'$timeout','$q',
 function($rootScope, $compile, $http, $timeout,$q) {
 
 var printHtml = function (html) {
 var deferred = $q.defer();
 var hiddenFrame = $('<iframe style="display: none"></iframe>').appendTo('body')[0];
 
 hiddenFrame.contentWindow.printAndRemove = function() {
 hiddenFrame.contentWindow.print();
 $(hiddenFrame).remove();
 deferred.resolve();
 };
 var htmlContent = "<!doctype html>"+
 "<html>"+
 '<head><link rel="stylesheet" type="text/css"
href="/style/css/print.css"/></head>'+
 '<body onload="printAndRemove();">' +
 html +
 '</body>'+
 "</html>";
 var doc = hiddenFrame.contentWindow.document.open("text/html", "replace");
 doc.write(htmlContent);
 doc.close();
 return deferred.promise;
 };
 var openNewWindow = function (html) {
 var newWindow = window.open("debugPrint.html");
 newWindow.addEventListener('load', function(){
 $(newWindow.document.body).html(html);
 }, false);
 };
 var print = function (templateUrl, data) {
 
 $rootScope.isBeingPrinted = true;
 $http.get(templateUrl).success(function(template){
 var printScope = $rootScope.$new()
 angular.extend(printScope, data);
 var element = $compile($('<div>' + template + '</div>'))(printScope);
 var waitForRenderAndPrint = function() {
 if(printScope.$$phase || $http.pendingRequests.length) {
 $timeout(waitForRenderAndPrint, 1000);
 } else {
 // Replace printHtml with openNewWindow for debugging
 printHtml(element.html());
 printScope.$destroy();
 }
 };
 waitForRenderAndPrint();
});
 };
 var printFromScope = function (templateUrl, scope, afterPrint) {
 $rootScope.isBeingPrinted = true;
 $http.get(templateUrl).then(function(response){
 var template = response.data;
 var printScope = scope;
 var element = $compile($('<div>' + template + '</div>'))(printScope);
 var waitForRenderAndPrint = function() {
 if (printScope.$$phase || $http.pendingRequests.length) {
 $timeout(waitForRenderAndPrint);
 } else {
 // Replace printHtml with openNewWindow for debugging
 printHtml(element.html()).then(function() {
 $rootScope.isBeingPrinted = false;
 if (afterPrint) {
 afterPrint();
 }
 });
 }
 };
 waitForRenderAndPrint();
 });
 };
 return {
 print : print,
 printFromScope : printFromScope
 }
 }
]);

Controller :


var template_url = '/views/print.client.view.html';
print_service.printFromScope(template_url,$scope,function(){
 // Print Completed
});


Conclusion

In this page (written and validated by ) you learned about AngularJS Print . What's Next? If you are interested in completing AngularJS tutorial, your next topic will be learning about: AngularJS ui router.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.