ng-repeat in ng-repeat with $ index selector

时间:2022-09-08 20:32:46

I want to select in the ng-class the form element, which is inside a ng-repeat, i need a 1:n selector in the ng-class. But the {{}} + $index is working correctly in the ng-class expression. The sector is parsed correct. But ng-class can not handle it. It is no detecting the selector, when {{}} + $index is inside.

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

Code:

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

  <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 个解决方案

#1


2  

The ng-class is already an expression, so you were putting 2 expressions inside eachother. You can fix it like this:

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

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

#1


2  

The ng-class is already an expression, so you were putting 2 expressions inside eachother. You can fix it like this:

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

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