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, where i'm have required field. I'm have form object $scope.prForm - where i'm see $error. Problem is in name="{{'qty_' + $index}}". In $scope.prForm i'm have field

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

but i'm need

qty_0: instantiate.c

How I can have good {{'qty_' + $index}} operation in name attribute?

user1005180
  • 81
  • 1
  • 1
  • 10

2 Answers2

9

Very easy:

name="qty_{{$index}}"

Here is a plunk to see how it work.

SET001
  • 11,480
  • 6
  • 52
  • 76
  • then i'm have qty_{{$index}}: instantiate.c, but i'm need qty_0: instantiate.c i'm use AngularJS v1.2.9 – user1005180 Feb 07 '14 at 15:33
  • 1
    Both your initial solution and this gives `qty_0`. Check your data-binding. – kubuntu Feb 07 '14 at 15:36
  • I am also in difficulty. Don't see problems. Just {{$index}} in name give me string '{{$index}}' - not '0' or '1' ... in my example ng-change="qtyPerKindCalc($index);" transmits to the function good $index, i.e. 0 or 1 ..., but in name i'm have string '{{$index}}' – user1005180 Feb 07 '14 at 15:44
  • @user1005180, I put a link to plnkr so that you can see this approach work like a charm. – SET001 Feb 07 '14 at 15:51
  • If i'm print in console $scope.prForm i'm have `instantiate.c { $error: Object, $name: "prForm", .... company: instantiate.c, job_name: instantiate.c, qty_{{$index}}: instantiate.c, __proto__: Ic` – user1005180 Feb 07 '14 at 15:53
  • Thanks - yes - it's works, but how i'm can to do validation in this case? If in form object i'm have field like: qty_{{$index}}: instantiate.c – user1005180 Feb 07 '14 at 15:58
  • [This doc](http://docs.angularjs.org/guide/forms) may be usefull for you. You can use binary $valid/$invalid to check validity. For form - use `prForm.$valid`, same for fields - `prForm.qty_0.$valid`. – SET001 Feb 07 '14 at 16:03
  • [link](http://plnkr.co/edit/4yeLiVDlA9XbNuXdbpKh?p=preview) Pls, see what i'm mean. – user1005180 Feb 07 '14 at 16:05
  • Yes, but in prForm i'm don't have qty_0, i'm have $qty_{{$index}}, then i'm can't to do check – user1005180 Feb 07 '14 at 16:06
1

Try this:

id="qty_{{$index}}"  

:)

Artyom Pranovich
  • 6,814
  • 8
  • 41
  • 60