Daniel
Daniel

Reputation: 137

AngularJS $compile template into iframe and print it

i'm making a directive to print a view teamplate with data from scope. My issue is with the iframe, I don't get to compile this. Template inject into iframe but $compile not bind scope data.

My code

angular.module('app').directive('printable', ['$templateCache', '$compile', function ($templateCache, $compile) {
return {
    restrict: 'EAC',
    scope: true,
    link: function (scope, element, attrs) {
        var iframe;
        var html = $templateCache.get(attrs.printView);
        var elm = document.createElement('iframe');

        elm.setAttribute('class', 'print-frame');
        elm.setAttribute('style', 'display: none;');

        element.parent().append(elm);

        iframe = element.parent().find('.print-frame')[0];

        var compiled = $compile(html);
        write(html);

        function write(value) {
            var doc;
            if (iframe.contentDocument) { // DOM
                doc = iframe.contentDocument;
            } else if (iframe.contentWindow) { // IE win
                doc = iframe.contentWindow.document;
            } else {
                alert('Wonder what browser this is... ' + navigator.userAgent);
            }

            doc.write(value);
            doc.close();
        }

        element.bind('click', function (event) {
            if (window.navigator.userAgent.indexOf("MSIE") > 0) {
                iframe.contentWindow.document.execCommand('print', false, null);
            } else {
                iframe.contentWindow.focus();
                iframe.contentWindow.print();
            }
        });
    }
};
}]);

Thanks in advance.

Upvotes: 2

Views: 2175

Answers (1)

Narek Mamikonyan
Narek Mamikonyan

Reputation: 4611

try like this

 var  compiled = $compile(html)(scope);

Upvotes: 2

Related Questions