AngularJS Form Validation Directive $setValidity on element

邮差的信 提交于 2019-12-18 10:23:00

问题


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}"

回答1:


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/



来源:https://stackoverflow.com/questions/15615071/angularjs-form-validation-directive-setvalidity-on-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!