元素上的 AngularJS 表单验证指令 $setValidity [英] AngularJS Form Validation Directive $setValidity on element

查看:27
本文介绍了元素上的 AngularJS 表单验证指令 $setValidity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对指令中的元素使用 $setValidity.我发现的所有示例似乎都将其设置在控制器上...

我在表单验证上分叉了一个 JS 小提琴,并尝试了很多东西.任何见解将不胜感激:http://jsfiddle.net/thomporter/pmKpG/2/

在小提琴中,$setValidity 在控制器上被调用:

ctrl.$setValidity('pwd', true);

我想做类似的事情:

elm.$setValidity('pwd', true);

以便在表单中我可以执行以下操作:

ng-class="{error:form.password.$error.pwd}"

解决方案

我想通了... 您必须在输入元素上有一个名称.一旦我添加了名称,错误就会根据需要自动绑定到元素,无需额外更改!

http://jsfiddle.net/thomporter/pmKpG/4/

I'm trying to use $setValidity on an element in a directive. All the examples I've found seem to set it on the controller...

I forked a JS fiddle on Form Validation and have tried a bunch of things. Any insights would be most appreciated: http://jsfiddle.net/thomporter/pmKpG/2/

In the fiddle, the $setValidity is called on the controller:

ctrl.$setValidity('pwd', true);

I'd like to do something like:

elm.$setValidity('pwd', true);

so that in the form I can do something like:

ng-class="{error:form.password.$error.pwd}"

解决方案

I figured it out... You have to have a name on the input elements. As soon as I added the name, the errors were automatically bound to the elements as desired, no additional changes needed!

<input ng-model="password"
       name="inputPassword"
       class="immediate-help"
       password-validate
       required
       type="password"
       id="inputPassword"
       placeholder="Password">

http://jsfiddle.net/thomporter/pmKpG/4/

这篇关于元素上的 AngularJS 表单验证指令 $setValidity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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