5

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?

Dusan
  • 791
  • 5
  • 16
user3676163
  • 129
  • 1
  • 5

2 Answers2

3

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);               
    ...
});
gkalpak
  • 47,844
  • 8
  • 105
  • 118
-1

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
}
Teemu Leisti
  • 3,750
  • 2
  • 30
  • 39