angular / angular.js

AngularJS - HTML enhanced for web apps!
https://angularjs.org
MIT License
58.81k stars 27.5k forks source link

ngMessages and array-like values form params #11669

Closed stryju closed 9 years ago

stryju commented 9 years ago

are there any plans for better support of "array-like" form params?

<input type=“number” name=“values[]” required
  ng-repeat=“value in someArray track by $index”
  ng-model=“someArray[$index]”>

now, none of those work:

the only working "hacky" solution would be something like [name=“values_{{$index}}”] + [ng-messages="values_{{$index}}"], but that strips the array-like url params functionality

kentcdodds commented 9 years ago

I'm not certain that I completely understand why you want this. The name attribute simply adds the NgModelController for that instance to the scope. Which you then pass to ng-messages. So, in an ng-repeat scenario, you have to make the name dynamic with interpolation so you don't get property overwrites--as you indicated (only works +1.3 by the way).

Personally, I think the way that it works currently is simple and adding some magic to accomplish what you're suggesting to angular's core would add unnecessary complexity. However, if you really want it, you could write a directive that would give you what you want. Something like array-name or something, which would require the ngModel controller, then push the controller to the property (array) passed. Shouldn't be too complicated. I think that'd probably be a better way to go about it.

stryju commented 9 years ago

i'm not suggesting to make it like that, just wanted to know the reasoning behind that.

AFAIK form elements with [name="someParam[]"] indicates that the given parameter is an array and should be interpreted as one (at least during my past adventure with PHP)

kentcdodds commented 9 years ago

Yeah, that's not the way it works in angular: https://docs.angularjs.org/api/ng/directive/input#usage

name (optional) string - Property name of the form under which the control is published.

stryju commented 9 years ago

fair enough :)