8

I wrote a directive which has a postLink, and I compiled some html code and render it by angularjs. But the problem is I can't get the html code from the generated DOM.

My code is:

app.directive("showResultAsText", ['$compile', function ($compile) {
    return {
        restrict: 'A',
         compile: function compile(tElement, tAttrs, transclude) {
            console.log('compile');
            var watchModal = tAttrs["showResultAsText"];
            var elem = jQuery(tElement);
            var oriHtml = elem.html();
            elem.empty();
            return {
                post: function postLink(scope, iElement, iAttrs, controller) {
                    scope.$watch(function () {
                        return scope.$eval(watchModal);
                    }, function (newVal) {
                        if (newVal) {
                            var s = $compile(angular.element(oriHtml))(scope);
                            console.log(s);
                            console.log(s[0]);
                            console.log(s[0].outerHTML);
                            console.log($('<div>').append(s[0]).html());
                            iElement.text(s[0].outerHTML);
                        }
                    });
                }
            }
        }
    }
}

You can see I used console.log to print the value of s. And in the console, it outputs:

enter image description here

You can see console.log(s) and console.log(s[0]) have many information, but when I use outerHTML, the inner buttons are all missing.

I also tried to use jquery: jQuery(s[0]).html(), but the same happened.

What is the correct way of convert that s to html code?

3
  • Can you explain why you need compiled html code? Actually keep in mind that $compile service do not only generate html, but also binds events to elements etc. So you can't re-use that html several times anyway. Commented Jan 28, 2013 at 16:41
  • I just want to show the generated html as text in the page, that I can copy it to somewhere else. Like a code generator Commented Jan 28, 2013 at 17:00
  • can you explain why you need a directive? if not absolutely necessary, it's not the angular-way to modify the DOM directly.. Commented Feb 16, 2013 at 18:24

1 Answer 1

4

It'd be easier if you could provide a plunkr / jsFiddle-example, but I think I know what's wrong. You're getting fooled by the console - when you you do console.log(s) it doesn't get logged immediately (it's async) - the console gets a reference to the dom element s[0], and by the time it's ready to print angular is done with it's rendering. On the other hand, s[0].outerHTML and s.html() are synchronous, so you get the pre-angular rendered result. To fix this you could just do a simple setTimeout:

setTimeout(function(){ console.log(s[0].outerHTML); });

Angular should now have time to do it's rendering before the callback (I don't think you need a delay, but I might be wrong). You can also use the angular $timeout-service which wraps the callback-function with $scope.$apply - see http://docs.angularjs.org/api/ng.$timeout

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.