这是plnker demo
在 script.js
中,我创建了一个变量 info
来存储选项。所选选项应为“西类牙语”,因为,
{
"name": "Spanish",
"value": 3082,
"selected": true //It's true here.
}
如何解决这个问题?
最佳答案
您需要添加ng-model
。根据您的 info.value.lang
,您可以将 selected : true
设置为模型值。
function MyCntrl($scope) {
$scope.info = info;
$scope.selectedItem = null;
angular.forEach(info.value.lang, function(lang) {
if (lang.selected === true){
$scope.selectedItem = lang.value;
}
});
}
<div ng-controller="MyCntrl">
<select ng-model="selectedItem" ng-options="option.value as option.name for option in info.value.lang" >
</select>
</div>
关于javascript - 使用 ng-options 生成下拉列表时,如何设置所选选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29642567/