简体   繁体   English

.html()返回空字符串

[英].html() returns empty string

I have the code: 我有代码:

        htmlObject = '<div status-box></div>';
        template = $compile(htmlObject)($scope);
        $scope.$digest();
        console.log(template);
        var templateAsHtml = template.html();
        console.log(templateAsHtml);

and the output: from the first console.log: 和输出:从第一个console.log:

{0: <div status-box="" class="ng-scope"></div>, length: 1}

and from the second: 从第二个开始:

''

It seems like the moment I call .html it just not converting it and it is empty. 似乎当我调用.html时,它只是不进行转换而为空。

When you ask for .html() of an element you will get the innerHTML of it. 当您要求元素的.html() ,您将获得它的innerHTML Since '<div status-box></div>' has no child nodes the behavior is correct. 由于'<div status-box></div>'没有子节点,因此行为是正确的。 If you want to return the HTML including the object itself you need to use outerHTML function. 如果要返回包含对象本身的HTML,则需要使用outerHTML函数。

var templateAsHtml = template[0].outerHTML;
console.log(templateAsHtml);

See docs: 查看文件:

Note: the [0] is used to access the vanilla JavaScript object inside of your wrapper object. 注意: [0]用于访问包装对象内部的原始JavaScript对象。

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

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