[styleguide] Naming convention for ng-template #40996
Comments
I like this suggestion. I've found myself occasionally wondering about some of these variables represent. I however suggest that if we steer away from the "*Ref" suffix because we have many kinds of Refs so the naming could become confusing. How about we instead use the "Template" suffix? And at that point, if we are making a recommendation for naming of ng-templates, we can as well make a recommendation for suffixes of elements, components, and directives. Here is a draft proposal: <!-- template variable reference for ng-template is suffixed with "Template" -->
<ng-template #usefulTemplate>
I'm super useful!
</ng-template>
<!-- template variable reference for components is suffixed with "Component" -->
<my-hello #helloComponent></my-hello>
<!-- template variable reference for DOM elements is suffixed with "Element" -->
<p #pElement>
Some interesting paragraph...
</p>
<!-- template variable reference for exported directives is suffixed with "{DirectiveName}Directive" -->
<form #profileNgFormDirective="ngForm">
</form> What do you think? Btw, if we go ahead with this change then we should also update the following guide to conform to the new style: |
I had forgotten about the others :) Perhaps some elements merit special treatment. <!-- template variable reference for input elements is suffixed with "Input" -->
<input #phoneInput placeholder="phone number"> Hmm maybe that's going overboard. The Now this isn't supported, but perhaps a <input #phone# placeholder="phone number">
<button (click)="callPhone(phone#.value)">Call</button> |
The suffix seems a bit magical. Why not just phoneInputElement? Also, for the purpose of distinguishing component properties from template references, what would a component do which takes a template as an input? Naming it |
FWIW I'm not a fan of Hungarian notation in variable naming. I don't really know that this is something that belongs in the Angular style guide versus something that individual teams can decide for themselves. |
Description
<ng-template #templateName>
are awesome! However it becomes a bit unclear if a referenced value is coming from the component or inside the template.Example
In the above example what is
bar
? Is it a field in the component or a template reference?When you're inside your editor the Angular Language Service makes this trivial to find out. However when reviewing a pull request it's not clear at all.
So I'd like to propose that we come up with a naming convention for
<ng-template>
.It can be something as simple as
{camelCaseName}Ref
inspired by theTemplateRef
typeThen our example becomes
What's the affected URL?
https://github.com/angular/angular/blob/master/aio/content/guide/styleguide.md
The text was updated successfully, but these errors were encountered: