0

I have this form:

<form (ngSubmit)="onSubmit()" #form="ngForm">    
    <div class="form-group">
      <label for="name">Name</label>
      <input type="text" name="name" ngModel id="name" class="form-control">
    </div>    
    ...
</form>

Inside the .ts file I reach the form with @ViewChild("form", {static:false}) myForm: NgForm

Now, starting from the field myForm, what is the best/easy way to reach and manipulate the other HTML elements inside the form, for example, the label element?

Thank you.

Matt
  • 265
  • 4
  • 17
  • You won't necessarily have what you want on the NgForm component itself. Have a read at this: https://stackoverflow.com/questions/39908967/how-to-get-reference-of-the-component-associated-with-elementref-in-angular-2/39909203#39909203 . If you want children elements, try one of these 2 SO post: https://stackoverflow.com/questions/47540610/how-to-get-elementref-reference-from-ngmodel-formcontrol-in-ngform-in-angular-4 . https://stackoverflow.com/questions/39642547/is-it-possible-to-get-native-element-for-formcontrol Hope this helps – Jojofoulk Mar 24 '21 at 06:38
  • Hi @Jojofoulk; I read the articles but it's not clear what you are suggesting to me. Should I add read:ElementRef to @ViewChild()?? In that case I won't have a NgForm anymore and, for example, the validation system of Angular won't work. – Matt Mar 24 '21 at 07:15

0 Answers0