-
Notifications
You must be signed in to change notification settings - Fork 27.4k
feat(ngName): allow interpolated name for form controls #2988
Conversation
ngName allows forms' fields to set their name dynamically, especially useful with ngRepeat. Right now is not possible to validate form controls like this: <input type="text" name="{{field.name}}" ng-model="field.data"> The current work-around is to use one nested form for each field but indeed it's an hack rather than a solution. This implementation could be moved inside the directive 'ngModel' or within its own directive 'ngName', I left intentionally it in the NgModelController because it's slightly faster, however I can refactor it as you prefer. this fixes also the issues angular#1404 and angular#2426
for instance <input type="text" name="{{field.name}}" ng-model="field.data"> with this patch would be: <input type="text" ng-name="field.name" ng-model="field.data"> |
+1 |
Hi, I think ngForm should also benefit from this improvement, as currently, the ng-form="static-name" and name="static-name" are not interpolated, meaning you cannot define a dynamic name for inner ng-form (again, this is especially needed for ng-form in ng-repeat loops), and, in the end, they won't be populating the parent form exported list of controls. |
👍 |
PR Checklist (Minor Feature)
|
@Pasvaz - can you make sure you sign the CLA? Thanks |
@petebacondarwin I think we should focus on #3135 while considering this PR. The #3135 has associated unit tests and is not observing the name attribute. |
I agree. Should we close this one? |
Yes imo. Working on 2 very similar prs doesn't make much sense and we shoul promote ones with unit tests. |
@Pasvaz - I think we will close this for now. Thank you for putting this PR together. Perhaps you could help us review and move the other PR forward? Thanks. |
@petebacondarwin yes I signed the CLA about a month ago. What about moving this code into its own directive ngName? I left it inside the ngForm for two reasons, it's faster and it takes care only of the ngName when it is inside a form, however I didn't like this approach, I would rather move it inside its own directive and provide also the feature to change the name of an element regardless if it is inside a form, having a directive ngName would allow me to make tests, docs and everything else. |
ngName allows forms' fields to set their name dynamically, especially useful with ngRepeat.
Right now is not possible to validate form controls like this:
<input type="text" name="{{field.name}}" ng-model="field.data">
The current work-around is to use one nested form for each field but indeed it's an hack rather than a solution.
This implementation could be moved inside the directive
ngModel
or within its own directivengName
, I left intentionally it in the NgModelController because it's slightly faster, however I can refactor it as you prefer.this fixes also the issues #1404 and #2426