2013-10-11 85 views
4

我有一组select-dropdown菜单,我试图根据angularJS中第一个select-dropdown的选择填充第二个select-dropdown。我不知道如何从这开始。AnglularJS:基于第一个select-dropdown的选择填充第二个select-dropdown

我已准备好所有模型,但我正在与动态人口挣扎。

选择1:

<select ng-model="selectedSource" ng-options="source.name for source in sourceList"> 
     <option value="">-- Select item --</option> 
    </select> 

$scope.sourceList= [ 
    { 
     "name":"Person", 
     "has": ["a","b","c"] 
    }, 
    { 
     "name":"Car", 
     "has": ["1","2","3"] 
    } 
]; 

什么想实现:

sourceList.namePerson填入第2个选择,下拉与targerSet1

$scope.targerSet1= [ 
    { 
     "name":"King Julien" 
    } 
]; 

sourceList.name我小号Car填入第二个选择,下拉与targerSet2

$scope.targerSet2= [ 
    { 
     "name":"RoyalMobile" 
    } 
]; 
+0

[可能的重复](http://stackoverflow.com/questions/16191060/filter-dropdown-with-subcategories-in-angular)。 –

+0

我知道它可能是重复的..但那个并没有真正的帮助。 –

+0

One controller,two models is the way to go https://stackoverflow.com/questions/18345020/populate-dropdown-2-based-on-dropdown-1-selection – AskPete

回答

7

你可以使用绑定变量的第一选择的第二个ng-options

选择

<select ng-model="selectedSource" ng-options="source.name for source in sourceList"> 
    <option value="">-- Select item --</option> 
</select> 

<select ng-model="selectedItem" ng-options="item.name for item in selectedSource.suboptions"> 
    <option value="">-- Select item --</option> 
</select> 

控制器

$scope.sourceList = [ 
    { 
     name: "Person", 
     suboptions: [ 
      { name: "King Julien" } 
     ] 
    }, 
    { 
     name: "Car", 
     suboptions: [ 
      { name: "RoyalMobile" } 
     ] 
    } 
]; 
相关问题