0

I have my form in $scope.myFormName which contains every NgModelController of every input field in there.

On error, I need to scroll to $invalid fields.

Thanks to this post I can access the list of the fields.

But now, I'd like to scroll up to the field. For this, I need to get the associated DOM element. So I do something like:

document.querySelector('name=['+inputName+']');

which feels dirty to me. Ain't there any pointer from the NgModel to its related DOM element? Or ain't this the angular way to do this?

Community
  • 1
  • 1
Augustin Riedinger
  • 20,909
  • 29
  • 133
  • 206
  • You can use jqulite for dom selection. Or if your elements are individual directives you have access to the dom element in the links element param. IMO this is a fine way of doing this though. – ajmajmajma Apr 05 '16 at 13:17
  • I feel Angular doesn't *want* us to do it this way. But I hardly can see an alternative there. – Augustin Riedinger Apr 05 '16 at 13:28
  • Why though? Angular isn't a dom manipulation library like jquery. – ajmajmajma Apr 05 '16 at 13:34
  • 1
    I also thought `document.querySelector` had low browser compatibility, otherwise what is the point of still using jQuery? But apparently it is quite compatible ... http://caniuse.com/#feat=queryselector – Augustin Riedinger Apr 05 '16 at 15:29
  • Jquery is just sugared javascript, so you're really just trading a bit bigger of a library for ease of use. What I mean by my comments though is there is nothing wrong with the way you're doing it and angular itself doesn't give you many tools for dom selection stuff. The ngmodel is best used when working with a controller and the controller shouldn't know about the DOM itself. So you use whatever dom selectors you like. Personally I think the raw javascript is the best way :). I suppose a bit more angular way would be using it's `angular.element`. But I don't think that matters too much. – ajmajmajma Apr 05 '16 at 15:40

0 Answers0