Angularjs ui-select:如何为多个下拉列表设置选定的值
问题描述:
我在页面上有多个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);
});
答
$scope.mappingStrengthList.selected = 'YOUR-VALUE-WHICH-YOU-WANT-SET'
答
如果你想要型动物选择的值,你的模型应该是不同的,现在你使用:ng-model="mappingStrengthList.selected"
上都。
我不认为你应该使用mappingStrengthList
这是填充选项的数据,模型应该是别的。
+0
我有动态填充下拉列表,我可以动态创建模型? –
+0
不,我的意思是,你需要为每个模型设置不同的模型,模型的值将会是所选选项的值,你应该像'ng-model =“myModel”'和'ng-模型= “myModel2”'。并在你的控制器中:'$ scope.myModel'和'$ scope.myModel2'。 – lithium
这将设置两个下拉列表的值... –
是啊,这是有道理的,你必须使每个模型的每个选择框,只有这样 –