【问题标题】:angular ngRepeat $index in name attribute名称属性中的角度ngRepeat $index
【发布时间】:2026-01-29 01:10:02
【问题描述】:
<div ng-repeat="fod in form.order_details">
...
    <td class="control-cell">
        <span ng-class="{error: prForm['qty_'+$index].$error.required && showValidationMessages}">
            <input type="number" name="{{'qty_' + $index}}" ng-model="fod.qty" ng-change="qtyPerKindCalc($index);" id="{{'qty_' + $index}}" required />
            <span ng-show="prForm['qty_'+$index].$error.required && showValidationMessages" class="error-msg">This field required</span>
        </span>
    </td>
...
</div>

ngRepeat,我有必填字段。我有表单对象 $scope.prForm - 我看到 $error。问题出在 name="{{'qty_' + $index}}" 中。在 $scope.prForm 我有字段

{{'qty_' + $index}}: instantiate.c

但我需要

qty_0: instantiate.c

如何在 name 属性中进行良好的 {{'qty_' + $index}} 操作?

【问题讨论】:

标签: javascript angularjs angularjs-ng-repeat


【解决方案1】:

很简单:

name="qty_{{$index}}"

这是一个plunk,看看它是如何工作的。

【讨论】:

  • 然后我有 qty_{{$index}}: instantiate.c,但我需要 qty_0: instantiate.c 我使用 AngularJS v1.2.9
  • 您的初始解决方案和这都给出了qty_0。检查您的数据绑定。
  • 我也有困难。看不到问题。只是名称中的 {{$index}} 给我字符串 '{{$index}}' - 而不是 '0' 或 '1' ...在我的示例中 ng-change="qtyPerKindCalc($index);"传输到函数good $index,即0或1 ...,但在名称中我有字符串'{{$index}}'
  • @user1005180,我放了一个指向 plnkr 的链接,这样你就可以看到这种方法的工作原理了。
  • 如果我在控制台打印 $scope.prForm 我有instantiate.c { $error: Object, $name: "prForm", .... company: instantiate.c, job_name: instantiate.c, qty_{{$index}}: instantiate.c, __proto__: Ic
【解决方案2】:

试试这个:

id="qty_{{$index}}"  

:)

【讨论】:

    最近更新 更多