4

In this example, I see the explanation but im still not sure i understand why in this case the #name template variable is set to "ngModel". What if there were 2 or 3 other input fields with template variables, would you also set their value to "ngModel"?

https://angular.io/guide/forms#show-and-hide-validation-error-messages

<label for="name">Name</label>
<input type="text" class="form-control" id="name"
       required
       [(ngModel)]="model.name" name="name"
       #name="ngModel">
<div [hidden]="name.valid || name.pristine"
     class="alert alert-danger">
  Name is required
</div>
Michael Kang
  • 52,003
  • 16
  • 103
  • 135
bitshift
  • 6,026
  • 11
  • 44
  • 108
  • https://stackoverflow.com/questions/45250259/what-is-auto-attribute-here-and-why-it-is-required `exportAs is a name under which the component instance is exported in a template` – yurzui Sep 30 '17 at 17:52

1 Answers1

1

ngModel is the NgModel directive's selector which you need to set to activate it. By setting #name="ngModel" you export the directive into the local variable with ngModel key. And each input can have it's own export, i.e.

...
<input type="text" class="form-control" id="name"
       required
       [(ngModel)]="model.name" name="name"
       #name="ngModel">
...
<input type="text" class="form-control" id="lastname"
       required
        [(ngModel)]="model.lastname" name="lastname"
        #lastname="ngModel">
Vega
  • 27,856
  • 27
  • 95
  • 103