我有一个打印服务,它具有获取传递模板URL和数据模板的功能。
I've got a printing service, it has a function that gets passed the template url and data for the template.
我需要以某种方式填充所提供的数据模板,然后在新窗口中打开它。下面是伪code代表我的想法:
I need to somehow populate that template with the data provided and then open it in a new window. Here's pseudo code for my idea:
PrintWindow.printModal = function(data, template) {
get(template).success(function(data) {
populatedTemplate = populate(template)
var mywindow = window.open('', '_blank');
mywindow.document.write('<html><head>');
mywindow.document.write('</head><body>');
mywindow.document.write(populatedTemplate);
mywindow.document.write('</body></html>');
});
return true;
};
我怎么能做到这一点?
想通了:
$http.get(template).success(function(data) {
var templateElement = angular.element('<div></div>');
templateElement.append(data);
var clonedElement = $compile(templateElement)($scope.data);
$timeout(function() {
var printWindow = window.open('', '_blank');
printWindow.document.write('<html><head>');
printWindow.document.write('</head><body>');
printWindow.document.write(clonedElement.html());
printWindow.document.write('</body></html>');
});
});