I use AngularJS 1.5, and I want to use ng-repeat to build a form.
Now, I have in my code :
<div class="form-group" ng-class="{'has-error': Form.field1.$dirty && Form.field1.$invalid}">
<label class="control-label"> Label 1 required <span class="symbol required"></span> </label>
<input type="text" class="form-control" ng-model="model.field1" ng-required="currentStep == 2" ng-blur="function1()">
<span class="error" ng-if="Form.field1.$dirty && Form.field1.$error.required">Field required !</span>
</div>
<div class="form-group">
<label class="control-label"> Label 2 not required </label>
<input type="text" class="form-control" ng-model="model.field2">
</div>
....
I want to use an array to buid my form like that :
$scope.myForm= [{
'type': 'text',
'name': 'field1',
'required': true,
'label': 'My field 1',
'ng': [{'ng-blur': 'function1'}]
}, {
'type': 'text',
'name': 'field2',
'required': false,
'label': 'My field 2',
}];
So I tried something like that :
<div ng-repeat="field in myForm" class="form-group {field.name}}" ng-class="{'has-error': Form.{{ field.name }}.$dirty && Form.{{ field.name }}.$invalid}">
<label class="control-label">{{ field.label }}</label>
<input type="text" class="form-control" name="{{ field.name }}" ng-model="adherent.{{ field.name }}" ng-required="currentStep == 2">
<span ng-if="{{ field.required }}" class="error text-small block" ng-if="Form.numeroSecuriteSociale.$dirty && Form.numeroSecuriteSociale.$error.required">Field required !</span>
</div>
But of course I got a syntax error... Do I have to create a directive ? or to use ng-init ?