AngularJS: SetValidity in ng-repeat fields

混江龙づ霸主 提交于 2019-12-11 17:15:36

问题


I have a form that generates some fields using ng-repeat. I want to attach each independent field to set validity in case it is left empty. Is there a way to do this?

Thanks!


回答1:


You need to use ng-form with your ng-repeat. I'd update your example, but you didn't give me one... so the idea is something like this:

<form name="myForm">
<div ng-repeat="item in items" ng-form="repeatForm">
  <input type="text" name="foo" ng-model="item.foo" required/>
  <span ng-show="repeatForm.foo.$error.required">required</span>
</div>
</form>

If you needed to access 'setValidity' on the model for some reason, you'd probably have to pass repeatForm.foo to a function either on your controller or on your scope. like ng-click="myWeirdValidationFunction(repeatForm.foo)". There's probably not much of a use case for this though.



来源:https://stackoverflow.com/questions/24290143/angularjs-setvalidity-in-ng-repeat-fields

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