0

Let's try again:

here's a working plunker: http://plnkr.co/edit/RTrdsLY8ONoeDLPYSFJi?p=preview

But the fields are connected each other.

when you look at DOM:

<input class="form-control ng-pristine ng-valid-maxlength ng-valid-minlength ng-valid ng-valid-required" type="text" name="fieldName" ng-model="fieldName" ng-minlength="3" ng-maxlength="20" required="">
                                                                                                                         ^^^^^^^^^^

of course! All have name="fieldName"!

But why!? it should be first_name, last_name and age!

Yes, there's a mistake in formField.html template:

 <input class="form-control" type="{{fieldType}}" name="fieldName" ng-model="fieldName" ng-minlength=3 ng-maxlength=20 required/>
                                                       ^^^^^^^^^^^

So let's change it into {{fieldName}}. And consequently all occurences of form.fieldName... into form.{{fieldName}}.

Well, where's gone red frame?

Let's look at DOM -

<input class="form-control ng-pristine ng-valid-maxlength ng-valid-minlength ng-valid ng-valid-required" type="text" name="first_name" ng-model="fieldName" ng-minlength="3" ng-maxlength="20" required="">

name is ok, but now has-error doesn't work...

Tomasz Brzezina
  • 1,452
  • 5
  • 21
  • 44
  • I think, that there's some problem with ng-model like here http://stackoverflow.com/questions/13714884/difficulty-with-ng-model-ng-repeat-and-inputs - but I can't get it work – Tomasz Brzezina Dec 18 '13 at 17:14

2 Answers2

1

Update: You can use a function like this to fetch your params from model. (accroding to your data structure)

Another forked plunker: http://plnkr.co/edit/mdKBoEffoUo2ilH7Jd3L?p=preview

$scope.getModels = function(items) {
  var params = {};

  if (angular.isArray(items)) {
    for (var i = 0; i < items.length; i++) {
      var item = items[i];

      if(angular.isArray(item.fields)) {
        for (var j = 0; j < item.fields.length; j++) {
          var field = item.fields[j];
          params[field.id] = field.model;
        }
      }
    }
  }

  alert(JSON.stringify(params, '', 4));
};

Since dynamic input name hasn't been implemented yet (Relatived PR here), I inserted a nested ng-form for validation. And because that, I think you have to manage form submittion yourself by using ngModel.

Check the forked exmaple here: http://plnkr.co/edit/8cP5YMKUGu6LfBCsRxAZ?p=preview

Template:

<div class="form-group" ng-form="form" ng-class="{true: 'has-error', false: 'has-success'}[form.fieldName.$invalid]">
    <label class="control-label col-sm-2" for="fieldName">{{fieldLabel}}</label>
    <div class="col-sm-6">
      <input class="form-control" type="{{fieldType}}" placeholder="enter valid name" name="fieldName" ng-model="fieldModel" ng-minlength=3 ng-maxlength=20 required/>
      <span class="help-block">
        <span ng-show="form.fieldName.$error.required">Required field</span>
        <span ng-show="form.fieldName.$error.minlength">Too few chars - min is (6)</span>
        <span ng-show="form.fieldName.$error.maxlength">Too much chars - max is (20)</span>
        &nbsp;
      </span>
    </div>
</div>

HTML:

<form-field ng-repeat="field in tabItem.fields" ng-model='field.model'
    field-type="field.type" field-name='field.id' field-label='field.label'>
</form-field>

JS:

KPNDirectives.directive("formField", function () {
    return {
      restrict: 'E',
      scope: {
        fieldModel: '=ngModel',
        fieldType: '=',
        fieldLabel: '=',
        fieldName: '='
      },
      templateUrl: 'formField.html'
    };   
});
Banana-In-Black
  • 2,558
  • 1
  • 27
  • 33
0

I'm not sure if your use of ng-class directive is right. Put the class name first and after the "variable". Try to use:

ng-class="'my-class-name': true"
Adrian Neatu
  • 1,989
  • 2
  • 19
  • 34