使用$ index选择器的ng-repeat中的ng-class

时间:2016-07-05 11:07:31

标签: javascript angularjs forms ng-repeat ng-class

我想在ng-class中选择form元素,它在ng-repeat中,我需要在ng-class中使用1:n选择器。但是{{}} + $索引在ng-class表达式中正常工作。扇区解析正确。但ng-class无法处理它。当{{}} + $ index在里面时,它没有检测到选择器。

代码:

<form name="vm.itemForm" ng-submit="vm.onSave()">
(...)
  <tr ng-repeat="item in vm.item.sizes | orderBy:predicate:reverse">
    (...)
     <div class="dc-input-group">
        <input class=""
               ng-model="item.stock"
               ng-class="{ 'testclass': vm.itemform.{{'stockInput'+$index}}.mySelector  }" 
               name="{{'stockInput'+$index}}" />
        </div>

HTML输出:

  <input class="form-validation dc-input dc-input--in-input-group dc-input--text-right ng-touched ng-not-empty ng-dirty ng-valid-number ng-valid ng-valid-required" 
    ng-model="item.stock"
    ng-class="{ 'testclass': vm.form.stockInput1.$valid }" 
    required=""
    name="stockInput1" 
    type="number"> 

1 个答案:

答案 0 :(得分:2)

ng-class已经是一个表达式,所以你在另外两个表达式中。你可以像这样解决它:

<input class=""
       ng-model="item.stock"
       ng-class="{'testclass': vm.itemform['stockInput'+$index].mySelector}" 
       name="{{'stockInput'+$index}}" />