无法使用ng-options删除空白选项 [英] unable to remove blank option with ng-options
问题描述
我对ng-options
有问题.
我无法摆脱ng-options select中的空白选项.
I have a problem with ng-options
.
I can't get rid of the blank option in my ng-options select.
我有2个选择对象,都带有ng-options
.第二选择的显示选项基于第一个选择.当我更改第一个选择的选项时,第二个选择最初显示空白选项.
I've got 2 select objects, both with an ng-options
. The displayed options of the second select is based on the first one. When I change the options of the first select, the second one initially displays a blank option.
我尝试了几种解决方案,但无法解决.
I tried several solutions but can't fix it.
我的对象
$scope.mySpecs = {
"1": {
"name": "Name 1",
"options": {
"1": "option 1",
"2": "option 2",
"3": "option 3"
}
},
"5": {
"name": "Name 5",
"options": {
"6": "option 6",
"7": "option 7",
"8": "option 8"
}
}
};
和我的两个选择:
<select ng-model="selectedSpecs.name" ng-options="k as v.name for (k, v) in mySpecs" ng-change="setVals()">
</select>
<select ng-model="selectedSpecs.option" ng-options="k as v for (k, v) in mySpecs[selectedSpecs.name].options"></select>
这是一个小提琴:
https://jsfiddle.net/byvpx7mn/3/
感谢您的帮助!
推荐答案
您可以使用Object.keys并选择第0个键来做一件事.
You can do one thing use Object.keys and choose the 0th key.
$scope.selectedSpecs.name = Object.keys($scope.mySpecs)[0];
如果您不希望第二秒为null,则将第一个选项设置为Fault.
And if you don't want null in second select set first option to fault.
<option value="" ng-if="false"></option>
这篇关于无法使用ng-options删除空白选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!