2014-02-07 26 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的索引變量可用時),我會嘗試並可能回報。