I am having trouble on making validations from fields coming from a ng-repeat. Once I satisfy an error, every field with the same is as well satisfied. Is there any other way to do this? Like using the ng-model as validation indicator instead of name?
Here is my code:
HTML
<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" name="fieldname" placeholder="Enter mobile number" required>
<span ng-hide="!myForm.fieldname.$error.required">THIS FIELD IS REQUIRED {{ myForm.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>
JS
var app = angular.module('angularjs-starter', []);
app.controller('MainCtrl', function($scope) {
$scope.choices = [{id: 'choice1'}, {id: 'choice2'}];
$scope.addNewChoice = function() {
var newItemNo = $scope.choices.length+1;
$scope.choices.push({'id':'choice'+newItemNo});
};
$scope.removeChoice = function() {
var lastItem = $scope.choices.length-1;
$scope.choices.splice(lastItem);
};
});
PS.
I am aware that this can be done by manipulating the name attribute of the input with incremental but I was wondering if this can be done with less effort
choice.idorchoice.nameas anameproperty. I don't see any other option. In order make validation work you need to have different names of the fields.