2014-02-07 25 views
4
<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 - 我在哪里看到$错误。问题在于name =“{{'qty_'+ $ index}}”。在$ scope.prForm我是有场

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

但我需要

qty_0: instantiate.c 

我怎么能有好{{ 'qty_' + $指数}}中的name属性操作?

+0

'name =“qty _ {{$ index}}”'? – SET

+1

动态名称解决方案:http://stackoverflow.com/a/12044600/1005180 – user1005180

回答

7

非常简单:

name="qty_{{$index}}" 

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

+0

然后我有qty _ {{$ index}}:instantiate.c,但我需要qty_0:instantiate.c 我'使用AngularJS v1.2.9 – user1005180

+1

你的初始解决方案和这给'qty_0'。检查你的数据绑定。 – kubuntu

+0

我也有困难。没有看到问题。只是在{{$ index}}的名字给我字符串'{{$ index}}' - 不'0'或'1'... 在我的例子ng-change =“qtyPerKindCalc($ index);”发送到函数good $ index,即0或1 ...,但名称我有字符串'{{$ index}}' – user1005180

1

试试这个:

id="qty_{{$index}}" 

:)

0

指令优先

我一直在追捕这个问题了几个小时,和我目前的分析是,它必须做与指令优先。 ng-repeat指令编译优先级为1000(大多数其他指令),但<input>指令编译优先级为0(默认值)。我认为"name"属性仅在编译<input>指令时才被评估,因此其他优先级为0或更高的指令可能会以您看到的名称获得未编译的"myName_$index"字符串。我有一个解决方法的想法(一个“索引” - 指令编译优先999,当ng-repeat的索引变量可用时),我会尝试并可能回报。