当输入名称为数组时,angularjs 表单无法引用输入控件 [英] angularjs form can not refer to input control when input name is array

查看:25
本文介绍了当输入名称为数组时,angularjs 表单无法引用输入控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 angularjs 测试表单验证时遇到问题

I encounter a problem when testing form validation with angularjs

根据 angularjs 表单指南

具有 ngModel 指令的输入控件包含 NgModelController 的实例.可以使用输入控件上的 name 属性将此类控件实例发布为表单实例的属性.

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.

我在 plunker 创建了测试代码,一切正常,直到我更改输入名字来自

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>

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

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

这是否意味着 angular 无法识别表单输入中的数组语法?

我的问题是我想保持正常的表单提交流程并且只使用 angular 进行表单验证,所以我需要将表单输入保留为数组以与后端表单处理一起使用

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

推荐答案

它与 Angular 无关.这是一个语法 JS 错误.

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

如果你想引用一个名为 user[age] 的属性,你应该这样做:

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

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

form1.user[age] 被错误地解释为 (form1.user)[age]

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

这篇关于当输入名称为数组时,angularjs 表单无法引用输入控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆