Hossain Muctadir
Hossain Muctadir

Reputation: 3626

Loop through child elements of $element

I need to loop thorough all child element of an $element while creating a custom directive. Here is the code for controller of the directive.

        controller: function ($element, $scope) {
        $scope.showChildElementData = function () {
            var sameLevelElems = $element.children());

            for (var i = 0; i < sameLevelElems.length; i++) {
                console.log(sameLevelElems[i].data());
            }
        };
    }

Function call to data returns error

TypeError: Object #<HTMLLIElement> has no method 'data'

But I can do

sameLevelElems.data()

Which always returns the first elements data. How can I loop through all children?

Upvotes: 2

Views: 2790

Answers (1)

Ivan Chernykh
Ivan Chernykh

Reputation: 42176

Angular's element has data method. So wrap your sameLevelElems[i] first , like this:

angular.element( sameLevelElems[i] ).data()

Upvotes: 1

Related Questions