I am a complete beginner with Angular and I have some conceptual doubts about the NgModel directive and its possibile use cases.
Reading on the Angular official documentation: https://angular.io/api/forms/NgModel
I can read only:
Creates a FormControl instance from a domain model and binds it to a form control element.
It seems to me that this FormControl should be an object holding the values inserted into my form fields and the related fields status (for example for validation pourpose). Is it correct? Now I have some doubt on concrete use cases.
So lets do an example.
I have this form:
<form (ngSubmit)="onSubmit(form)" #form="ngForm">
<div class="form-group">
<label for="name">Character Name</label>
<input
type="text"
id="name"
name="name"
class="form-control"
ngModel
required
#nameCtrl="ngModel">
<span class="help-block" *ngIf="nameCtrl.invalid && nameCtrl.touched">Please, enter a name!</span>
</div>
<div class="form-group">
<label for="side">Chose Side</label>
<select name="side" id="side" class="form-control" ngModel>
<option *ngFor="let option of availableSides" [value]="option.value">
{{ option.display }}
</option>
</select>
</div>
<button class="btn btn-primary" type="submit" [disabled]="form.invalid">Add Character</button>
</form>
On my parent form element I have: #form="ngForm". What exactly indicates? From what I can understand it is simply creating a reference of the entire form into the form "object"\reference.
Then I have this input field inside my form:
Here ngModel appear twice:
Why I have a first ngModel? What it exactly means?
The second time that appear is #nameCtrl="ngModel" that should simply create a reference of this input field into the nameCtrl "address".