How to use angular.element to find and iterate over multiple elements?

user3676163 picture user3676163 · Jun 14, 2014 · Viewed 7.9k times · Source

I am writing a directive in AngularJS and one of the things I need to do is find all of the controls inside a form and iterate over them.

If jQuery was loaded I do this:

var inputs = element.find(".ng-invalid,.ng-valid");
inputs.each(function ()
{
    var i = $(this);               

});

But since it is not loaded and I just have Angular I can only think to do something like:

var inputs = element.find("input");
//for loop thru this
var selects = element.find("select");
//for loop thru this
//....etc

Is there some better way using jQLite to accomplish this?

Answer

gkalpak picture gkalpak · Jun 15, 2014

If supporting IE7 or older is not a requirement, you can use the querySelectorAll method of Element:

var controls = element[0].querySelectorAll('.ng-invalid, .ng-valid');
[].forEach.call(controls, function (ctl) {
    var c = angular.element(ctl);               
    ...
});