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

2
  • 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 docs.angularjs.org/api/ng/directive/input Commented Nov 20, 2015 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. Commented Nov 20, 2015 at 12:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.