Search code examples
javascriptangularjscheckboxangularjs-ng-repeatangular-ngmodel

Angular ngRepeat dynamically create ngModel with $index


I have this html:

And I want to create dynamically ng-models for these inputs:

     <div class="order" ng-repeat="order in newOrders">
          <input type="checkbox"  ng-model="model$index" />{{$index}} please check
     </div>

and it's not working. I there a way to dynamically create ng-model in a input, inside a ng-repeat?

EDIT:

The final result is:

  <input type="checkbox"  ng-model="model$index" />0 please check

My expected result is:

  <input type="checkbox"  ng-model="model0" />0 please check

Solution

  • The way you are trying to do its not appropriate, creating separate variable for each element, that will make more maintenance sort of work.

    Also this will become messy to when you apply any filter on ng-repeat. Instead of that you could place that model value inside the ng-repeat element which is order here order.

     <div class="order" ng-repeat="order in newOrders">
         <input type="checkbox"  ng-model="order.value" />{{$index}} please check
     </div>