简体   繁体   English

具有$ index选择器的ng-repeat中的ng-class

[英]ng-class in ng-repeat with a $index selector

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. 我想在ng-class中选择form元素,该元素位于ng-repeat中,我需要在ng-class中使用1:n选择器。 But the {{}} + $index is working correctly in the ng-class expression. 但是{{}} + $ index在ng-class表达式中正常工作。 The sector is parsed correct. 该部门解析正确。 But ng-class can not handle it. 但是ng-class无法处理它。 It is no detecting the selector, when {{}} + $index is inside. 当{{}} + $ 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: 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"> 

The ng-class is already an expression, so you were putting 2 expressions inside eachother. ng-class已经是一个表达式,因此您要在彼此之间放入2个表达式。 You can fix it like this: 您可以这样解决:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM