I have a view where users can see a partial view of they're total messages. I want to have a button that prints the entire history of messages.
The steps as I see them would be a directive that on click gets the history of messages, compiles a template with this data in the scope, and prints it. But I've been trying for some time now and have scoured the internet trying different suggestions from injecting into an iframe, a hidden div, etc. etc. but with no success.
Am I going about this the wrong way?
[UPDATE] Here's where i'm at. {{Message}} is never interpolated.
'use strict';
angular.module('casengo').directive('printMessages', [
'$log', '$window', '$compile', '$templateCache', 'CaseService', function($log, $window, $compile, $templateCache, CaseService) {
var printResults = function (mywindow) {
mywindow.print();
mywindow.close();
};
return {
restrict: 'A',
scope: {
'case': '='
},
link: function(scope, el, attr) {
el.on('click', function () {
var mywindow = window.open('', 'my div', 'height=400,width=600');
mywindow.document.write('<html><head>');
mywindow.document.write('</head><body >');
mywindow.document.write('<div id="printed" ng-repeat="message in scope.case.messages">{{message}}</div>');
mywindow.document.write('</body></html>');
var printContent = angular.element(mywindow.document.getElementById('printed'));
if (scope.case.hasPrevious) {
CaseService.loadPreviousMessages(scope.case.id, scope.case.messages[0].id, 0).then(function (res) {
scope.case.hasPrevious = res.hasPrevious;
if (res.messages) {
Array.prototype.unshift.apply(scope.case.messages, res.messages);
}
$compile(printContent.contents())(scope);
printResults(mywindow);
});
} else {
$compile(printContent.contents())(scope);
printResults(mywindow);
}
});
}
};
}
]);