1

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
  • 1
    What are you trying to gain by setting a dynamic name on an element? Commented Apr 21, 2014 at 20:31

1 Answer 1

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]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.