我正在尝试创建一个下拉菜单,但是当它加载时,我得到一个空的下拉选择框。
我正在使用以下 html:
<tr ng-repeat="i in data.suggestions">
<td>
<select ng-model="i.selYears" ng-options="selYears for val in selYears"></select>
</td>
</tr>
有了这些数据:
[
{
"years": [1, 2, 5]
},
{
"years": [1, 2, 5]
},
{
"years": [1, 2, 5]
}
]
是我的数据结构有问题,还是 ng-options
有问题?
这是我希望最终输出的样子:
<tr>
<td>
<select>
<option value="1">1 year</option>
<option value="2" selected="selected">2 years</option>
<option value="5">5 years</option>
</select>
</td>
</tr>
<tr>
<td>
<select>
<option value="1">1 year</option>
<option value="2" selected="selected">2 years</option>
<option value="5">5 years</option>
</select>
</td>
</tr>
<tr>
<td>
<select>
<option value="1">1 year</option>
<option value="2" selected="selected">2 years</option>
<option value="5">5 years</option>
</select>
</td>
</tr>
最佳答案
下面的代码应该可以工作
<select ng-options="y.years as y.years for y in selYears" ></select>
假设 selYears 是 $scope 上的一个属性,例如
$scope.selYears = [
{
"years": [1, 2, 5]
},
{
"years": [1, 2, 5]
},
{
"years": [1, 2, 5]
}
关于javascript - ngOptions 创建空下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31948841/