This question was asked at least two times before Question 1 Question 2 but I thing that the answer was not complete many people use static name with ng-repeat, it's a possible solution but in my case dont work because I need the name of the input in the controller to pass how parameter to another directives, one better idea is using the directive dinamicName
but with this I don't have idea how show the errors
the following is my code example
<form name="myForm" class="form-horizontal" role="form" ng-submit="submitForm()">
<div ng-repeat="field in data.fields">
<ng-form name="form">
<!-- Texto plano -->
<div class="col-sm-6">
<input type="{{ field.type }}" data-ng-model="field.data" class="form-control" required dynamic-name="field.name"/>
<span ng-show="form."+{{field.name}}+".$touched && form."+{{field.name}}+".$error.required" class="help-block with-errors" >Required!</span>
</div>
</div>
</ng-form>
</div>
</form>
the directive dinamicName:
angular.module('formModule')
.directive('dynamicName',
function dynamicNameDirective ($compile, $parse) {
return {
restrict: 'A',
terminal: true,
priority: 100000,
link: function(scope, elem) {
var name = $parse(elem.attr('dynamic-name'))(scope);
// $interpolate() will support things like 'skill'+skill.id where parse will not
elem.removeAttr('dynamic-name');
elem.attr('name', name);
$compile(elem)(scope);
}
};
});
this code dont show the spam message
Thanks