0

This is my list in the controller:

vm.areaList.push({"areaId":"101","areaName":"A","subAreaNames":["AA","BB"
                     ,"CC","DD","EE","FF","GG","HH"]});
vm.areaList.push({"areaId":"102","areaName":"B","subAreaNames":["II","JJ" ]});
        vm.areaList.push({"areaId":"103","areaName":"Administration","subAreaNames":["KK","LL","MM"]});

And this is the first dropdown on my html page:

<select multiple="multiple" data-ng-options="area.areaId as area.areaName for area in AreaReportCtrl.areaList" 
             data-ng-model="AreaReportCtrl.selectedArea">
            </select>

Based on the value selected from this dropdown I want to populate another dropdown with the corresponding sub-areas list.

Is it possible to keep the area and sub area data in single array ? If yes, how shall I iterate it in ng-options of the next dropdown based on the selected area? Or should I store the sub-area list in a different array by keeping a reference of the areaId ?

6
  • You can create a new sub area list based on areaId or when you change the area call a method and initialise the sub areas to another variable and the itterate with the newly assigned value Commented Oct 18, 2017 at 9:21
  • I was looking for a solution to iterate the same list in the second dropdown also if possible and was trying to avoid calling a method on selection if that is possible Commented Oct 18, 2017 at 9:24
  • how are you expecting this to work with select multiple? are you wanting to aggregate all the possible sub-areas? Commented Oct 18, 2017 at 9:33
  • @Claies, user can select multiple areas and all the corresponding sub areas should be displayed in another list Commented Oct 18, 2017 at 9:34
  • 1
    so then that's a yes, you want to aggregate the sub-areas? that won't be possible without firing some function. Commented Oct 18, 2017 at 9:35

1 Answer 1

1
<select data-ng-options="area.areaId as area.areaName for area in AreaReportCtrl.areaList" 
             data-ng-model="AreaReportCtrl.selectedArea">
            </select>

<select data-ng-options="subAreaName as subAreaName  for area.subAreaNames in AreaReportCtrl.areaList|filter:{areaId:AreaReportCtrl.selectedArea}" 
             data-ng-model="AreaReportCtrl.selectedArea">
            </select>
Sign up to request clarification or add additional context in comments.

3 Comments

this won't work with select multiple, since selectedArea will be an array.
you need multiple means you need to fire an event
Ok got it. When I try your code for single selection, I get this error: [ngOptions:iexp] Expected expression in form of 'select (as label)? for (key,)?_value_ in collection' but got 'subareaName as subareaName for area.subareaNames in AreaReportCtrl.areaList|filter:{areaId:AreaReportCtrl.selectedArea}'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.