0

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

Community
  • 1
  • 1
oriaj
  • 768
  • 1
  • 16
  • 33
  • Use `dynamic-name="field.data"` instead of field.name as you want to send the input value. In the directive use the params to get the value. Have a look at https://docs.angularjs.org/api/ng/directive/input – Ionut Milas Nov 20 '15 at 12:20
  • Try to keep the encapsulation of the directive as high as possible. Don't use scope. Go with param and pass the param to be used to the directive instead of picking them from main scope. – Ionut Milas Nov 20 '15 at 12:22

0 Answers0