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?

5
  • 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. Commented Apr 5, 2016 at 13:17
  • I feel Angular doesn't want us to do it this way. But I hardly can see an alternative there. Commented Apr 5, 2016 at 13:28
  • Why though? Angular isn't a dom manipulation library like jquery. Commented Apr 5, 2016 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 ... caniuse.com/#feat=queryselector Commented Apr 5, 2016 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. Commented Apr 5, 2016 at 15:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.