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?
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.
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.
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.
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.
<input type="text" ng-model="q.orderNo" ng-init="q.orderNo = $index">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With