问题描述:

<div class="col-lg-6" ng-repeat="controls in steps.infos">

<div class="form-group">

<label class="control-label" for="store_list">{{controls.label}}</label>

<input type="{{controls.type}}" class="form-control input-lg mandatory" id="{{controls.id}}" ng-model="formData[controls.id]" value="{{controls.value}}" name="control_{{controls.id}}" ngRequired="{{controls.mandatory}}">

</div>

</div>

网友答案:

You can create a directive that will take in you control as input and modify the template accordingly.

It is similar to this

AngularJs: Customizing the template within a Directive

EDIT:

<div ng-repeat="content in vm.contents">

  <form-el formId="content.id"
           type="content.type"
           label="content.label"
           name="content.label"
           data="content.data"
    ></form-el>
</div>

directive ::

 .directive('formEl', function () {
    return {
      restrict: 'E',
      compile: function(element, attrs) {
        var type = attrs.type || 'text';
        var required = attrs.hasOwnProperty('required') ? "required='required'" : "";
        if(type=='text'){
          var htmlText = '<div class="control-group">' +
            '<label class="control-label" for="' + attrs.formId +   '">' + attrs.label + '</label>' +
            '<div class="controls">' +
            '<input type="' + type + '" class="input-xlarge" id="' + attrs.formId + '" name="' + attrs.name + '" ' + required + '>' +
            '</div>' +
            '</div>';
        } else if(type=='radio'){
          var htmlText = '<div class="control-group">' +
            '<label class="control-label" for="' + attrs.formId + '">' + attrs.label + '</label>' +
            '<div class="controls">';
          for(var i in attrs.data){
            htmlText=htmlText+'<input type="' + type + '" class="input-xlarge" id="' + attrs.formId + '" name="' + attrs.name + '" ' + required + '>';
          }
          htmlText=htmlText + '</div>' +  '</div>';
        } else if(type=='selectoption'){
          //add html for radio button
        }
        element.replaceWith(htmlText);
      }
    };
  });
相关阅读:
Top