Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
76
rated 0 times [  77] [ 1]  / answers: 1 / hits: 120259  / 10 Years ago, mon, july 7, 2014, 12:00:00

I am working in a directive and I am having problems using the parameter element to find its childs by class name.



.directive(ngScrollList, function(){
return {
restrict: 'AE',
link: function($scope, element, attrs, controller) {

var scrollable = element.find('div.list-scrollable');

...
}
};
})


I can find it by the tag name but it fails to find it by class name as I can see in the console:



element.find('div')
[<div class=​list-viewport>​…​</div>​,<div class=​list-scrollable>​…​</div>​]
element.find('div.list-scrollable')
[]


Which would be the right way of doing such thing? I know I can add jQuery, but I was wondering if wouldn't that be an overkill....


More From » angularjs

 Answers
19

jQlite (angular's jQuery port) doesn't support lookup by classes.



One solution would be to include jQuery in your app.



Another is using QuerySelector or QuerySelectorAll:



link: function(scope, element, attrs) {
console.log(element[0].querySelector('.list-scrollable'))
}


We use the first item in the element array, which is the HTML element. element.eq(0) would yield the same.



FIDDLE


[#70287] Friday, July 4, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
stefanicarolinat

Total Points: 145
Total Questions: 91
Total Answers: 93

Location: Cambodia
Member since Thu, Oct 7, 2021
3 Years ago
stefanicarolinat questions
Mon, Nov 15, 21, 00:00, 3 Years ago
Fri, Apr 16, 21, 00:00, 3 Years ago
Thu, Oct 15, 20, 00:00, 4 Years ago
Fri, Jul 17, 20, 00:00, 4 Years ago
;