【问题标题】:Using ng-disabled inside of an ng-repeat在 ng-repeat 中使用 ng-disabled
【发布时间】:2016-02-29 19:14:41
【问题描述】:

我有一个无线电输入,其中包含一个已禁用 ng-repeat 的 ng-repeat,它会触发一个内部有 if else if 块的函数。如果满足其中一个条件,则返回布尔值。我的问题是,如果满足条件,我只希望禁用某些无线电。目前,如果满足条件,将为数组中的所有项目设置布尔值。如何在 ng-repeat 中为 ng-disabled 运行函数并满足每个项目的条件?

【问题讨论】:

  • 不描述问题,添加代码:)
  • 例如,如果您的ng-repeat 超过item in items,您只需将item 传递给ng-disabled 函数,并为其返回布尔值。

标签: javascript angularjs


【解决方案1】:

您必须将 item 或重复 $index 传递给您的检查器函数以进行区分,如下所示:

<input ng-repeat="item in yourItems" type="radio" ng-disabled="yourChecker($index, item)">

在控制器js中:

$scope.yourChecker = function(index, item){
    if(index==x){
       // TODO: return state here
    }

    // Or check item property

    if(item.x){

    }
}

【讨论】:

    【解决方案2】:
    <form>
      <input type="radio" ng-repeat="element in array track by $index" ng-disabled="isDisabled(element)">{{element}}</input>
    </form>
    

    在您的范围内定义isDisabled,并使用任何给定element 的属性返回一个布尔值。如果你想根据索引来做,你也可以将$index传递给函数。

    【讨论】:

      猜你喜欢
      • 2016-09-06
      • 1970-01-01
      • 2018-02-08
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      • 2012-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多