angularjs form can not refer to input control when input name is array

若如初见. 提交于 2019-12-06 14:29:30

问题


I encounter a problem when testing form validation with angularjs

According to angularjs form guide,

an input control that has the ngModel directive holds an instance of NgModelController. Such a control instance can be published as a property of the form instance using the name attribute on the input control.

I created test code at plunker, it all works fine untill I change the input name from

<input type="number" name="age" ng-model="user.age" max="100" required>

<p>{{form1.age.$error}}</p>

to

<input type="number" name="user[age]" ng-model="user.age" max="100" required>

<p>{{form1.user[age].$error}}</p>

Does this mean angular can not recognize array syntax in form input?

The problem for me is I want to keep the normal form submission flow and only use angular for form validation, so I need to keep form input as array to work with the backend form handling


回答1:


It has nothing to do with Angular. It is a syntactic JS error.

If you want to reference a property named user[age], you should do it like this:

form1['user[age]'].$error

form1.user[age] is incorrectly interpreted as (form1.user)[age]



来源:https://stackoverflow.com/questions/23205314/angularjs-form-can-not-refer-to-input-control-when-input-name-is-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!