Chr*_*ris 1 javascript angularjs angularjs-directive ng-options
我在服务中得到了这些变量:
months: [
{ label: 'January', value: 1, disabled: true },
{ label: 'February', value: 2, disabled: true },
{ label: 'March', value: 3, disabled: true },
{ label: 'April', value: 4, disabled: false },
{ label: 'May', value: 5, disabled: false },
{ label: 'June', value: 6, disabled: false },
{ label: 'July', value: 7, disabled: false },
{ label: 'August', value: 8, disabled: false },
{ label: 'September', value: 9, disabled: false },
{ label: 'October', value: 10, disabled: false },
{ label: 'November', value: 11, disabled: false },
{ label: 'December', value: 12, disabled: false }
],
currentMonth: 4
Run Code Online (Sandbox Code Playgroud)
我的选择看起来像这样:
<select name="goalStartMonth" id="goalStartMonth" class="form-control gb-select" ng-model="startMonth" ng-change="frequencyUpdated()" ng-options="month.value as month.label for month in months"></select>
Run Code Online (Sandbox Code Playgroud)
这可以很好地建立<select>,但我想禁用当月之前的月份(不能选择过去一个月)
ng-options的Angular文档显示:
"禁用数组中的值时禁用标签"
所以我试过了:
<select name="goalStartMonth" id="goalStartMonth" class="form-control gb-select" ng-model="startMonth" ng-change="frequencyUpdated()" ng-options="month.value as month.label disable when month.value < currentMonth for month in months"></select>
Run Code Online (Sandbox Code Playgroud)
这打破了<select>- 没有选项被渲染.
我也尝试过:
<select name="goalStartMonth" id="goalStartMonth" class="form-control gb-select" ng-model="startMonth" ng-change="frequencyUpdated()" ng-options="month.label disable when month.value < currentMonth for month in months"></select>
Run Code Online (Sandbox Code Playgroud)
同样的结果.
我在这里错过了什么?
要做到这一点的方法之一是使用ng-repeat和ng-disabled上<option>内<select>,就像这样:
<select name="goalStartMonth" id="goalStartMonth" class="form-control gb-select" ng-model="startMonth" ng-change="frequencyUpdated()">
<option ng-repeat="month in months" ng-disabled="month.value < currentMonth" >{{month.label}}</option>
</select>
Run Code Online (Sandbox Code Playgroud)
在这里工作JSFiddle
编辑
如上所述,此功能在1.4.0测试版之前无法使用.这是一个JSFiddle,显示它使用1.4.4-beta.6版本的AngularJS