I'm trying to simplify how I generate my forms with AngularJS.
My ultimate goal is to be able to write something like:
<form>
<field-div label="Name">
<field which="form.name"></field>
</field-div>
<field-div label="Language">
<field which="form.language"></field>
</field-div>
</form>
For this, I'm working with two directives (fieldDiv and field) and two JavaScript objects: one that represents the data being edited in the form, and another that represents the form definition (field type, field options...).
See this JSFIDDLE for the code: http://jsfiddle.net/vincedo/9Uf6C/
After banging my head against the wall a few times, I think I got it working. The main difficulty was to get Angular to treat a string -- that I got from my form definition object (e.g. "entity.name") -- as a scope property with two-way data binding.
I still have two questions:
- Is it the right way to do it? More specifically, my code uses two
scope.$watchper field, and I'm afraid it might affect performance. - In my form, why isn't "English" selected in the "Language" field? It must be a stupid mistake: the HTML source uses 0, 1, 2... as
<option>values whereas my code uses string keys such asen,fr... But after trying to wrap my head around $wrap and $parse for so many hours, I don't see it. :-)
Thanks!
$scope.formobject so that only the "name" key remains?