angularjs form can not refer to input control when

2019-04-15 07:23发布

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条回答
Rolldiameter
2楼-- · 2019-04-15 08:13

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]

查看更多
登录 后发表回答