3
votes

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>
1
stackoverflow.com/questions/45250259/… exportAs is a name under which the component instance is exported in a templateyurzui

1 Answers

1
votes

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">