I have an an array of objects in javascript:
[
{id: 1, name: "Item 1", subItems: []},
{id: 2, name: "Item 2", subItems: []},
{id: 3, name: "Item 3", subItems: [[
{id: 4, name: "Item 3.1"},
{id: 5, name: "Item 3.2"},
{id: 6, name: "Item 3.3"}]}
]
Using angularJS 1.2 (I need to support IE8) i would like to create:
<select>
<option value="1">Item 1</option>
<option value="1">Item 1</option>
<optgroup label="Item 3">
<option value="4">Item 3.1</option>
<option value="5">Item 3.2</option>
<option value="6">Item 3.3</option>
</optgroup>
</select>
So I need to loop over the top level items creating an option for each but for those with sub items I need to create an option group then loop over the subitems creating an option for each.
In any other language I would do a loop with an if statement to determine what element to use, however in angular you seem to have to create an element for a ng-repeat. In html you can't wrap each <option>
with another element (<div>
or <span>
). So I'm stuck.
I need an element-less ng-repeat. Can anyone suggest any solution?