AngularJS在ng repeat上的验证表单
我在对来自ng repeat的字段进行验证时遇到问题。一旦我满足了一个错误,每个字段都同样满足。还有别的办法吗?比如使用ng模型作为验证指标而不是名称 这是我的密码: HTML 附言AngularJS在ng repeat上的验证表单,angularjs,validation,Angularjs,Validation,我在对来自ng repeat的字段进行验证时遇到问题。一旦我满足了一个错误,每个字段都同样满足。还有别的办法吗?比如使用ng模型作为验证指标而不是名称 这是我的密码: HTML 附言 我知道这可以通过使用incremental操作输入的name属性来完成,但我想知道这是否可以用更少的精力来完成,我认为如果不在name字段中添加增量,您就无法完成。 你可以用这样的东西很容易做到 <input type="text" ng-model="choice.name" name="fieldname
我知道这可以通过使用incremental操作输入的name属性来完成,但我想知道这是否可以用更少的精力来完成,我认为如果不在name字段中添加增量,您就无法完成。 你可以用这样的东西很容易做到
<input type="text" ng-model="choice.name" name="fieldname_{{$index}}" required>
如果不在名称字段中添加增量,我认为您无法完成此操作。 你可以用这样的东西很容易做到
<input type="text" ng-model="choice.name" name="fieldname_{{$index}}" required>
这应该可以做到-注意我是如何将字段名更改为-field+$index的,这样我们就可以知道ng repeat中每个迭代之间的差异
<div ng-app="angularjs-starter" ng-controller="MainCtrl"> <form name="myForm" novalidate > <fieldset data-ng-repeat="choice in choices">
<input type="text" ng-model="choice.name" ng-init="vm.name='field'+$index" name="{{vm.name}}" placeholder="Enter mobile number" required>
<span ng-hide="!myForm.{{vm.name}}.$error.required">THIS FIELD IS REQUIRED {{ myForm.{{vm.name}}.$error.required }}</span>
<button class="remove" ng-show="$last" ng-click="removeChoice()">-</button> </fieldset> <button class="addfields" ng-click="addNewChoice()">Add fields</button>
<div id="choicesDisplay">
{{ choices }} </div> </div>
此字段是必需的{{myForm.{vm.name}}.$error.REQUIRED}
-添加字段
{{choices}}
这应该可以做到-注意我是如何将字段名更改为-field+$index的,这样我们就可以知道ng repeat中每个迭代之间的差异
<div ng-app="angularjs-starter" ng-controller="MainCtrl"> <form name="myForm" novalidate > <fieldset data-ng-repeat="choice in choices">
<input type="text" ng-model="choice.name" ng-init="vm.name='field'+$index" name="{{vm.name}}" placeholder="Enter mobile number" required>
<span ng-hide="!myForm.{{vm.name}}.$error.required">THIS FIELD IS REQUIRED {{ myForm.{{vm.name}}.$error.required }}</span>
<button class="remove" ng-show="$last" ng-click="removeChoice()">-</button> </fieldset> <button class="addfields" ng-click="addNewChoice()">Add fields</button>
<div id="choicesDisplay">
{{ choices }} </div> </div>
此字段是必需的{{myForm.{vm.name}}.$error.REQUIRED}
-添加字段
{{choices}}
此字段是必需的{{nestedMyForm.fieldname.$error.REQUIRED}
-
添加字段
{{choices}}
检查我是如何添加数据的ng form=“nestedMyForm”希望它能起作用
此字段是必需的{{nestedMyForm.fieldname.$error.REQUIRED}
-
添加字段
{{choices}}
检查我是如何添加数据的ng form=“nestedMyForm”希望它能起作用我会使用
choice.id
或choice.name
作为name
属性。我看不到任何其他选择。为了进行验证,您需要有不同的字段名称。好的,谢谢,如果有任何其他选择,我会使用choice.id
或choice.name
作为name
属性来测试我的运气。我看不到任何其他选择。为了进行验证工作,您需要有不同的字段名称。好的,谢谢,如果有其他选择,请测试我的运气您更快:)想要给出相同的答案此字段是必需的{{myForm.fieldname.$error.REQUIRED}}这也是此部分的代码吗?我尝试了您的建议,但没有成功。将名称设置为动态不会正确检测验证。奇怪。你更快:)想要给出相同的答案此字段是必需的{{{myForm.fieldname.$error.REQUIRED}}这也是此部分的代码吗?我尝试了你的建议,但没有成功。将名称设置为动态不会正确检测验证。奇怪。@Dean Christiam Armada(拇指支撑):)@Dean Christiam Armada(拇指支撑):)
<div ng-app="angularjs-starter" ng-controller="MainCtrl">
<form name="myForm" novalidate >
<fieldset data-ng-repeat="choice in choices" data-ng-form="nestedMyForm" >
<input type="text" ng-model="choice.name" name="fieldname" placeholder="Enter mobile number" required>
<span ng-hide="!nestedMyForm.fieldname.$error.required">THIS FIELD IS REQUIRED {{ nestedMyForm.fieldname.$error.required }}</span>
<button class="remove" ng-show="$last" ng-click="removeChoice()">-</button>
</fieldset>
<button class="addfields" ng-click="addNewChoice()">Add fields</button>
<div id="choicesDisplay">
{{ choices }}
</div>
</div>