I have an angular 2 app that heavily uses forms throughout the application. Most of the forms are built using the reactive forms module in Angular, but the API I am working against also have lots of "dynamic fields".
For instance, the "back-end" allows users to create custom fields for certain posts/pages and I want to offer users the ability to use them in my Angular 2 app as well.
Example
The API gives me a JSON list that looks like this:
{
"id": "the-custom-field-id",
"label": "The input label",
"min": 4,
"max": 8,
"value": "The current value of the custom field"
},
...
Right now, I fetch the list of custom fields in an observable and use ngfor
to loop them and produce form elements for each entry, like this:
<div *ngFor="let cf of _customFields" class="form-group">
<label>{{cf.label}}</label>
<input id="custom-field-{{cf.id}}" type="text" class="form-control" value="{{cf.value}}">
</div>
Then on submit, I reach into the DOM (using jQuery) to get the values using the "IDs" of the custom fields.
This is ugly and goes against the idea of not mixing jQuery and Angular.
There must be a way of integrating these dynamic forms into Angular instead, so I can use them with control groups and validation rules?