Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS$将模板编译成iframe并打印出来_Angularjs_Angularjs Directive - Fatal编程技术网

AngularJS$将模板编译成iframe并打印出来

AngularJS$将模板编译成iframe并打印出来,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我正在做一个指令,用scope中的数据打印一个view teamplate。我的问题是iframe,我不能编译它。模板注入iframe,但$compile不绑定范围数据 我的代码 angular.module('app').directive('printable', ['$templateCache', '$compile', function ($templateCache, $compile) { return { restrict: 'EAC', scope: true,

我正在做一个指令,用scope中的数据打印一个view teamplate。我的问题是iframe,我不能编译它。模板注入iframe,但$compile不绑定范围数据

我的代码

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();
            }
        });
    }
};
}]);
提前谢谢。

像这样试试

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

我尝试了var compiled=$compile(html)(范围);var compiled=$compile(html);var elm=已编译(范围);写作(榆树);视图绑定继续为空:(@Daniel你能把它放在小提琴里吗?我上传了一个例子到Plunkr,上面有我的问题:对不起,在你的例子中我看不到你提到的问题所有绑定都是工作的。对不起,我复制了错误的Plunkr url。这是正确的:点击按钮后,会出现一个新窗口,但没有绑定范围值。