gpt4 book ai didi

javascript - 使用 select 作为标签时,如何使用 ng-options 禁用 angularjs 中的选项

转载 作者:行者123 更新时间:2023-11-30 07:23:18 24 4
gpt4 key购买 nike

我在服务中有这些变量:

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

我的选择看起来像这样:

<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>

构建 <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>

这打破了 <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>

相同的结果。

我在这里错过了什么?

最佳答案

一种方法是使用 ng-repeatng-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>

工作 JSFiddle here

EDIT

如上所述,此功能直到 1.4.0 测试版才可用。 Here是一个 JSFiddle,显示它可以使用 AngularJS 的 1.4.0-beta.6 版本

关于javascript - 使用 select 作为标签时,如何使用 ng-options 禁用 angularjs 中的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29497567/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com