3

I am getting some data back from my server. The data structure is:

[{"sectorName1": "nameHere",
  "subSectors": ["sub1", "sub2", "sub3"]
 },
 {"sectorName2": "nameHere",
  "subSectors": ["sub1", "sub2", "sub3"]
 }]

I am trying to display each sectors subSectors with ng-options. So when some uses the dropdown they will see all the subsectors.

I have tried this but doesn't seem to work:

<select id="selectSubSector" class="form-control" name="subSector" ng-model="item" ng-options="sec for sec in mySectors.subSectors ">
</select>

where mySectors is the data that comes back from the server.

Any suggestions?

10
  • What does not work? console errors? no options in the select? Commented Feb 3, 2016 at 9:46
  • @JohannesJander no options. Select remains empty Commented Feb 3, 2016 at 9:47
  • and what is mySectors? Could it be you wanted to write mySectors[0].subsectors? Commented Feb 3, 2016 at 9:48
  • @JohannesJander mySectors is the data that comes from the server. It contains the objects as shown in the data structure in the question. Those objects then contain a nested subSectors array. And that what I want, that nested subSectors array. Commented Feb 3, 2016 at 9:49
  • Well, then you need to use mySectors[0].subsectors if you do not want to aggregate the subsectors of several sectors - which is just a simple map operation Commented Feb 3, 2016 at 9:52

4 Answers 4

3

I have created a jsfiddle, have updated your response data as well:

HTML:

<div ng-app="app" ng-controller='TestCtrl'>
    <select ng-model="selectData">
        <option value="">Select</option>
        <optgroup ng-repeat='item in data' label="{{item.sectorName}}">
            <option ng-repeat="option in item.subSectors">{{option}}</option>
        </optgroup>
    </select>
</div>

JS

var app = angular.module('app', []);

app.controller('TestCtrl', function ($scope) {
    $scope.data = [{
        "sectorName": "nameHere1",
        "subSectors": ["sub1", "sub2", "sub3"]
    },
     {
         "sectorName": "nameHere2",
         "subSectors": ["sub1", "sub2", "sub3"]
     }];
});
Sign up to request clarification or add additional context in comments.

3 Comments

Amazingly done. So simple! No idea why I couldn't think of it. Nice one mate!
Thankx dude. Request to please accept the answer, if it works for your case
optgroup is very helpful. Thx.
1

i have create a plunker:https://plnkr.co/edit/3QkxdT6P8upwhwttUSDc?p=preview

the js code:

  $scope.mySectors = [{
    "sectorName1": "nameHere",
    "subSectors": ["sub1", "sub2", "sub3"]
  }, {
    "sectorName2": "nameHere",
    "subSectors": ["sub1", "sub2", "sub3"]
  }];

  $scope.subSectors = new Array();
  for (var i = 0; i < $scope.mySectors.length; i++) {
       for(var j=0; j< $scope.mySectors[i].subSectors.length; j++){

    $scope.subSectors.push($scope.mySectors[i].subSectors[j]);
       }
  }

the html code:

<select id="selectSubSector" class="form-control" name="subSector" ng-model="item" ng-options="sec for sec in subSectors"></select>

1 Comment

Thank you so much. Nice technique, I should've thought of filleting the data in the controller before sending it to the view. Learned something new. Cheers mate. +1
0

mySectors is an array. So there is no mySectors.subSectors. You have to specify index for mySectors. For example mySectors[i].subsectors

Comments

0

I was trying to do the same thing and a solution to just display the sub-sections by using ng-if="false" or style="display: none" and ng-repeat-start/end.

<div ng-repeat="sector in Sectors">
<select>
    <option ng-repeat-start="subSectors in sector.subSectors" ng-if="false"></option>
    <option ng-repeat-end ng-repeat="subSectorValue in subSectors" value="{{ subSectorValue }}">{{ subSectorValue }}</option>
</select>
</div>

This gives us a select box with the following options.

  • sub1
  • sub2
  • sub3
  • etc

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.