AngularJS element.innerHTML is undefined from within directive
Asked Answered
A

1

15

Let's say I have:

directives.directive('foo', function () {
    return {
        restrict:'A',
        scope: true,
        link:function (scope, element, attr) {

            console.log('innerHTML is ' + element.innerHTML);

            scope.$watch('update', function (newValue) {
                console.log('innerHTML is... ' + element.innerHTML);
            });

        }
    }
});

... then innerHTML is undefined. I imagine this is due to the way Angular processes the DOM. What is the right way to obtain the innerHTML?

Animism answered 10/2, 2013 at 17:25 Comment(0)
I
40

The element variable that is passed to your link function is a jqLite object - not a DOM object. You can obtain the DOM object with element[0] (like you could in jQuery), but jqLite provides a method for you: element.html(). Check out the docs.

Incoordinate answered 10/2, 2013 at 20:3 Comment(1)
Thank you. Exactly what I was missing.Animism

© 2022 - 2024 — McMap. All rights reserved.