简体   繁体   English

$ compile返回对象中的html

[英]$compile return html in object

so im calling a html template and want to bind the data with angular, so i get the data to bind, i get the html, when i try to compile it will return all the html binded but in (i think) object, what can i do to make it html. 所以我调用了一个html模板,想用角度绑定数据,所以我得到要绑定的数据,我得到了html,当我尝试编译时,它将返回所有绑定的html,但是在(我认为)对象中,可以我这样做使其成为html。

This is the code 这是代码

$.get("file.html", function(partial){

        var scope = $rootScope.$new();
        scope.data = result;

        var el = angular.element(partial);

        var compiled = $compile(el)(scope);
        var finalHtml = el[0];


        $timeout(function(){
            var calendar = window.open();
            calendar.document.write(finalHtml);
            calendar.focus();
            calendar.print();
        });
    });

I already try .html .toString String() nothing worked 我已经尝试过.html .toString String()无效

Thank you in Advance 先感谢您

Your compiled variable is an angular jQuery or jqlite element that can be inserted into your document. 您的已compiled变量是可以插入文档中的有角jQuery或jqlite元素。 If you want to get the html for it, you can use use the outerHTML attribute on the underlying node (you get the underlying node by grabbing the first array element compiled[0] ) - https://developer.mozilla.org/en-US/docs/Web/API/Element/outerHTML 如果要为其获取html,则可以在基础节点上使用outerHTML HTML属性(通过获取第一个已compiled[0]数组元素compiled[0]获得基础节点)-https: //developer.mozilla.org/en -US / docs / Web / API / Element / outerHTML

var compiled = $compile(el)(scope);
// scope.$digest() // only call if not within an angular $digest already
$timeout(function() {
  var finalHtml = compiled[0].outerHTML;
  ...
}

According to the documentation "After linking the view is not updated until after a call to $digest which typically is done by Angular automatically." 根据文档“链接后,只有在调用$ digest之后,视图才会更新,这通常是由Angular自动完成的。” so you either have to manually call scope.$digest() or actually use one the angular API to do the request using either $http or preferably using $templateRequest like @ThinkingMedia suggested. 因此,您必须手动调用scope.$digest()或实际上使用angular API使用$http或最好使用$templateRequest来执行请求,例如@ThinkingMedia建议。 After the angular $digest has run, then you can access the updated view. 在有角$ digest运行之后,您可以访问更新的视图。

I created a plunker here that shows how it all works properly using just the AngularJS api: http://plnkr.co/edit/rFcfgB3FWhsfyySfr0rU?p=preview 我在这里创建了一个插件,展示了如何仅使用AngularJS api正常运行: http ://plnkr.co/edit/rFcfgB3FWhsfyySfr0rU?p=preview

I also changed how the popup is opened a bit to deal with the security implication of doing popups. 我还更改了打开弹出窗口的方式,以应对执行弹出窗口的安全隐患。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM