Reputation: 3626
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
Reputation: 42176
Angular's element
has data
method. So wrap your sameLevelElems[i]
first , like this:
angular.element( sameLevelElems[i] ).data()
Upvotes: 1