2016-05-29 35 views
0

我在頁面上有多個UI選擇。如何分別爲每個項目設置選定值,從而使用從服務中檢索的單個列表?Angularjs ui-select:如何爲多個下拉列表設置選定的值

HTML:

<div class="col-sm-4"> 
    <ui-select class="col-sm-12 no-padding" ng-model="mappingStrengthList.selected"> 
    <ui-select-match placeholder="Choose strength"> 
    <span ng-bind="$select.selected.name"></span> 
    </ui-select-match> 
    <ui-select-choices repeat="item in (mappingStrengthList | filter: $select.search) track by item.id"> 
    <span ng-bind="item.name"></span> 
    </ui-select-choices> 
    </ui-select> 
</div> 
<div class="col-sm-4"> 
    <ui-select class="col-sm-12 no-padding" ng-model="mappingStrengthList.selected"> 
    <ui-select-match placeholder="Choose strength"> 
    <span ng-bind="$select.selected.name"></span> 
    </ui-select-match> 
    <ui-select-choices repeat="item in (mappingStrengthList | filter: $select.search) track by item.id"> 
    <span ng-bind="item.name"></span> 
    </ui-select-choices> 
    </ui-select> 
</div> 

控制器:

ListService.getList('mapping-strength').then(function (results) { 
       $scope.mappingStrengthList = results; 
      }, function (error) { 
       console.log(error); 
      }); 

enter image description here

回答

0
$scope.mappingStrengthList.selected = 'YOUR-VALUE-WHICH-YOU-WANT-SET' 
+0

這將設置兩個下拉列表的值... –

+0

是啊,這是有道理的,你必須使每個模型的每個選擇框,只有這樣 –

1

如果你想要型動物選擇的值,你的模型應該是不同的,現在你使用:ng-model="mappingStrengthList.selected"上都。

我不認爲你應該使用mappingStrengthList這是填充選項的數據,模型應該是別的。

+0

我有動態填充下拉列表,我可以動態創建模型? –

+0

不,我的意思是,你需要爲每個模型設置不同的模型,模型的值將會是所選選項的值,你應該像'ng-model =「myModel」'和'ng-模型= 「myModel2」'。並在你的控制器中:'$ scope.myModel'和'$ scope.myModel2'。 – lithium

相關問題