Last active
June 14, 2016 23:35
-
-
Save estevanio/19519a36156e6457a4fc94216e6fee36 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app= angular.module('app', ['ngCordova']); | |
app.factory('PrintSvc', ['$rootScope', '$compile', '$http', '$timeout', '$q', '$cordovaPrinter', function($rootScope, $compile, $http, $timeout, $q, $cordovaPrinter) { | |
var printUrl = function(templateUrl, data) { | |
$http.get(templateUrl).success(function(template) { | |
var printScope = $rootScope.$new(); | |
angular.extend(printScope, data); | |
var element = $compile(angular.element('<div>' + template + '</div>'))(printScope); | |
var waitForRenderAndPrint = function() { | |
if (printScope.$$phase || $http.pendingRequests.length) { | |
$timeout(waitForRenderAndPrint); | |
} else { | |
// console.log("should print"); | |
$cordovaPrinter.print(element.html()); | |
printScope.$destroy(); | |
} | |
}; | |
waitForRenderAndPrint(); | |
}); | |
}; | |
var printTmpl = function(template, data) { | |
var printScope = $rootScope.$new(); | |
angular.extend(printScope, data); | |
var element = $compile(angular.element('<div>' + template + '</div>'))(printScope); | |
var waitForRenderAndPrint = function() { | |
if (printScope.$$phase || $http.pendingRequests.length) { | |
$timeout(waitForRenderAndPrint); | |
} else { | |
// console.log("should print"); | |
$cordovaPrinter.print(element.html()); | |
printScope.$destroy(); | |
} | |
}; | |
waitForRenderAndPrint(); | |
}; | |
return { | |
printUrl: printUrl, | |
printTmpl: printTmpl | |
}; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment