Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set value of ngmodel using index in ng-repeat

Tags:

angularjs

I have this array and iterate the value using ng-repeat. Using ng-repeat I would be able to use $index which represent the iterator.

Before I have this code where the user input the question order number:

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

But the client requested for a draggable feature, to drag drop the questions to be sorted out. eg. the user dragged Question #1 to Question #2 place then their place will change thus the question order number will re-index. With this the user input for order number won't be needed no more but I still have to set the q.orderNo and bind the $index to it when I passed to my 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 to q.orderNo, how would I do that?

like image 773
Wondering Coder Avatar asked Sep 26 '13 08:09

Wondering Coder


People also ask

How do I get the index of an element in NG-repeat?

Note: The $index variable is used to get the Index of the Row created by ng-repeat directive. Each row of the HTML Table consists of a Button which has been assigned ng-click directive. The $index variable is passed as parameter to the GetRowIndex function.

How do you condition in NG-repeat?

You can add condition using ng-if also and this is effective too. You can apply any condition to your list using ng-if attribute. In below example, I have put condition where Age > 20 and IsActive is true. ng-repeat will fetch all records which full fill this scenario.

How do you bind a value to ngModel?

If you have a one-way binding to ngModel with [] syntax, changing the domain model's value in the component class sets the value in the view. If you have a two-way binding with [()] syntax (also known as 'banana-in-a-box syntax'), the value in the UI always syncs back to the domain model in your class.

What is [( ngModel )]?

The ngModel directive is a directive that is used to bind the values of the HTML controls (input, select, and textarea) or any custom form controls, and stores the required user value in a variable and we can use that variable whenever we require that value. It also is used during form validations.


1 Answers

<input type="text" ng-model="q.orderNo" ng-init="q.orderNo = $index">
like image 189
AlwaysALearner Avatar answered Sep 27 '22 20:09

AlwaysALearner