Skip to content

Instantly share code, notes, and snippets.

@estevanio
Last active June 14, 2016 23:35
Show Gist options
  • Save estevanio/19519a36156e6457a4fc94216e6fee36 to your computer and use it in GitHub Desktop.
Save estevanio/19519a36156e6457a4fc94216e6fee36 to your computer and use it in GitHub Desktop.
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