Ngmodel value using index in ng-repeat

I have this array and repeat the value with ng-repeat. Using ng-repeat, I could use $ index, which represents an iterator.

Before I get this code, in which the user enters the question order number:

<div ng-repeat="q in entries"> <input type="text" ng-model="q.orderNo"> </div> 

But the client requested a drag-and-drop feature to drag questions that need to be sorted. eg. the user dragged question No. 1 to question No. 2, then their place will change, so the order number of the question will be re-indexed. With this, user input of the order number is no longer needed, but I still need to install q.orderNo and bind the index $ index to it when I switched to api.

 <div ng-repeat="q in entries"> <input type="text" ng-model="q.orderNo = $index"> //this is what I want to accomplish </div> 

I want to assign the index $ q.orderNo, how would I do it?

+8
angularjs
source share
1 answer
 <input type="text" ng-model="q.orderNo" ng-init="q.orderNo = $index"> 
+13
source share

All Articles