1

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:

{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.

2
  • 3
    var templateAsHtml = template[0].outerHTML;, why? Commented Feb 19, 2016 at 10:58
  • 1
    html() gets innerHtml so you have to use outer one Commented Feb 19, 2016 at 11:01

1 Answer 1

4

When you ask for .html() of an element you will get the innerHTML of it. Since '<div status-box></div>' has no child nodes the behavior is correct. If you want to return the HTML including the object itself you need to use outerHTML function.

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.