Clarification on Forms Section: Show and Hide Validation Error Messages
- Overview of the Issue - The guide is attempting to explain the importance of setting a variable to "ngModel"; however, the point is lost because the example confuses the reader
- Motivation for or Use Case - I'm trying to implement the new, new forms methodology. I am encountering an "ExportAs" "ngForm" exception. There's at least this stackoverflow: http://stackoverflow.com/questions/38648407/angular2-error-there-is-no-directive-with-exportas-set-to-ngform complaining about an "ExportAs" error.
- Angular Version(s) - 2.0.0
- Reproduce the Error - From the guide:
<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>Why "ngModel"? A directive's exportAs property tells Angular how to link the reference variable to the directive. We set name to ngModel because the ngModel directive's exportAs property happens to be "ngModel".
Now we can control visibility of the "name" error message by binding properties of the name control to the message <div> element's hidden property.
- Suggest a Fix - I wish I could. But, the example contains a lot of references to "name"! So, which "name" is it? Is ngModel set to this one: name="name" or this one id="name" or this one: [(ngModel)]="model.name"?? It's impossible to know. Please spice things up and change up some of these names to something else. Get creative. Then, I think it'll be clear which name the guide is referring to.
Thanks for the documentation. All in all, you guys have done well to keep up with all of the changes.
FWIW...
id="name"is the HTML id; AFAIK angular doesn't use it for anything, it's just there to give thelabelsomething to point itsforattribute to[(ngModel)]="model.name"is what you're binding the control to - i.e. thenameproperty of themodelproperty of your componentname="name"is the HTML field name, which AFAIK angular just uses to register the control w/ the parent form or group (i.e. so you can take an instance of ngForm and get a specific control by name)#name="ngModel"is an angular template variable, namednameand referencing the instance of the ngModel directive associated with this control. This is thenameyou see used in the[hidden]="name.valid || name.pristine"expression.
That said, I tend to agree that the naming can very easily get confusing, particularly when you have both template variables and component properties with the same(ish) names. Maybe this is something that should be addressed in the style guide and then implemented throughout the examples?