This is my first question, so please be gentle on me! I hope you can help.
I am trying to define a select box (dropdown) within my HTML view using ng-repeat, so that I can create a select for each item in an array. I have successfully achieved this, but my problems come when I try to get the options I need into each of the selects.
The array I am using{
"field1": 1,
"field2": "Value01",
"field3": "Value02",
"refFields": [
{
"fieldId": 100,
"fieldValue": "fieldValueA"
},
{
"fieldId": 101,
"fieldValue": "fieldValueB"
}
]
}, etc...
I need a select per item in the 'top level' of the array, and in each select box the values that should appear should be the fieldValue from the refFields nested array.
My current (broken) code products some pretty rubbish results. Here's the code:
<div ng-repeat="fk in fks">
<ng-form name="fksForm">
<select name="fkselect" ng-model="fk" ng-options="item as item.refFields for item in fks">
</select>
</ng-form>
</div>
Could anybody provide me with some insight into whether this is possible? I don't think that flattening the data is an option for this particular application. I've tried quite a few different things including passing an index to the ng-options, which as you might expect produced some pretty dodgy results. I also tried this, which doesn't give me any useful output.
ng-options="item as item.refFields.fieldValue for item in fks"
Here's my JSFiddle: ng-option nested JSON Array within ng-repeat
Many thanks in advance for any suggestions!
With Underscore.js you could prepare your data for ng-options.
For me it looks pretty complicated. I would re-think if you could modify your data structure. I can't recommend a structure because I don't understand how the final result will look like.
I've created two arrays one with the field names that's used for the
ng-repeat
and the second array holds therefFields
for the options.With
$index
you can get the current index of theng-repeat
to access therefFields
.I'm not sure if I've created this correctly because I don't know what you're doing with the
field1
,field2
,field3
values.Please have a look at this jsfiddle or the demo below (same code).