user3676163
user3676163

Reputation: 129

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

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?

Upvotes: 5

Views: 8346

Answers (2)

Teemu Leisti
Teemu Leisti

Reputation: 3770

Use a for-loop.

var inputs = element.find(".ng-invalid,.ng-valid");
for (var index=0; index < inputs.length; index++) {
  var subElement = angular.element(inputs)[index];
  // do something with subElement
}

Upvotes: -1

gkalpak
gkalpak

Reputation: 48211

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);               
    ...
});

Upvotes: 3

Related Questions