如何禁用 Angular JS 中的选项? [英] How to disable option in Angular JS?

查看:30
本文介绍了如何禁用 Angular JS 中的选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 ng-repeat 选项:

<option ng-repeat="year in data.dateList.year" value="{{year.id}}" ng-disabled="(year.id < 2015) ? true : false>

你可以看到 ng-disabled="(year.id < 2015) ? true : false"

如果 year.id 小于 2015,为什么我的选项没有被禁用?

这是我的新代码:

<option ng-repeat="year in data.dateList.year" ng-disabled="(year.id) < formData.beginYear[$index]">{{年.值}}</选项>

解决方案

这里需要使用 ng-options 来禁用选项

</选择><选择 ng-model="年"ng-options="year.id disable when (year.id </选择>

演示 Plunkr

I have ng-repeat option:

<option ng-repeat="year in data.dateList.year" value="{{year.id}}" ng-disabled="(year.id < 2015) ?  true : false">

You can see ng-disabled="(year.id < 2015) ? true : false"

Why my option is not disabled if year.id less than 2015?

This is my new code:

<div class="itm" ng-repeat="a in range(num) track by $index">
   <option ng-repeat="year in data.dateList.year" ng-disabled="(year.id) < formData.beginYear[$index]">
      {{year.value}}
   </option>
</div>

解决方案

You need to use ng-options here which has way to disable the options

<select ng-model="year"
  ng-options="year.id disable when (year.id < 2015) for year in data.dateList.year">
</select>

Update

For two input field depends each other, you can do it like below. Main problem was while you were doing comparison it was string, as value attributes convert the value to string format. You could use ng-options that does preserves the dataType of the value & will make more easier while making comparison.

Markup

<select ng-model="yr[$index]" 
   ng-options="year.id as year.id for year in years">
</select>

<select ng-model="year" 
   ng-options="year.id disable when (year.id < yr[$index]) for year in years">
</select>

Demo Plunkr

这篇关于如何禁用 Angular JS 中的选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆