在AngularJS形式动态验证和名称 [英] Dynamic validation and name in a form with AngularJS
问题描述
我有这种形式: http://jsfiddle.net/dfJeN/
正如你可以看到输入的名称值是静态设置:
As you can see the name value for the input is statically set :
name="username"
,表单验证工作正常(添加东西,从输入中删除所有文字,一个文本必须出现)。
, the form validation works fine (add something and remove all text from the input, a text must appears).
然后我尝试动态设置名称值: http://jsfiddle.net/jNWB8/
Then I try to dynamically set the name value : http://jsfiddle.net/jNWB8/
name="{input.name}"
然后,我将此我的验证
Then I apply this to my validation
login.{{input.name}}.$error.required
(这种格局将在NG-重复使用),但我的表单验证坏了。这是我的浏览器PTED正确间$ P $(如果我检查,我看到login.username的元素。error.required $)。
(this pattern will be used in an ng-repeat) but my form validation is broken. It is correctly interpreted in my browser (if I inspect the element I saw login.username.$error.required).
任何想法?
编辑:在控制台登录的范围后,将出现它的
After logging the scope in the console it appears that the
{{input.name}}
EX pression不插。我的表单作为{{input.name}}属性,但没有用户名。
expression is not interpolate. My form as an {{input.name}} attribute but no username.
更新:由于1.3.0-rc.3名={{input.name}}的作品如预期请参阅的#1404
推荐答案
您不能做你想要做这样的东西。
You can't do what you're trying to do that way.
假设你正在试图做的是你需要动态元素添加到形式,像一个NG重复,你需要使用嵌套的 NG-表单让那些个别项目的验证:
Assuming what you're trying to do is you need to dynamically add elements to a form, with something like an ng-repeat, you need to use nested ng-form to allow validation of those individual items:
<form name="outerForm">
<div ng-repeat="item in items">
<ng-form name="innerForm">
<input type="text" name="foo" ng-model="item.foo" />
<span ng-show="innerForm.foo.$error.required">required</span>
</ng-form>
</div>
<input type="submit" ng-disabled="outerForm.$invalid" />
</form>
不幸的是,它只是不折角一个证据充分的功能。
Sadly, it's just not a well-documented feature of Angular.
这篇关于在AngularJS形式动态验证和名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!