1

I am trying to get the innerHTML of an element with dynamic content form within a directive.

Below is the Plunker:

https://plnkr.co/edit/rXVin1LPWzpH4eMqZc7R?p=preview

The one in green is the expected result. The one in red is the part I hope to get working.

Notice on the console.log result, the outcome for el.innerHTML is {{ item }}. Why isn't it getting the text that is bound to it? How do I get it to work?

2
  • 3
    Try add setTimeout into the directive. Commented Jan 4, 2017 at 6:42
  • Thanks, @digit! That's one way of doing it. plnkr.co/edit/8vGmylxVOUerWctZOF7q?p=preview Commented Jan 4, 2017 at 7:52

1 Answer 1

4

It's because Angular hasn't finished processing the child templates from ng-repeat.

You can warp the code in scope.$evalAsync to make it work:

link: function(scope, element, attr, content) {

  scope.$evalAsync(function() {

    var el = element[0];

    // Code
  });
}

More on $evalAsync vs $timeout: https://stackoverflow.com/a/17303759/2887841

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

1 Comment

Thanks. Works great!

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.