Tags
Angular
Asked 8 years ago
4 Oct 2016
Views 1094
debugger

debugger posted

ngRepeat inputs not bind to (parent) model

when we change input value . it dont bind to parent model

<body  ng-app >
<div ng-init="cartdetail= ['1', '2', '3']">
{{cartdetail}}
<ul><li ng-repeat="num in cartdetail">
       <input ng-model="num"></input>
    </li>
</ul>

</div>
</body>

changes to the content are not reflected in the model , hope some have solution
Mahesh Radadiya

Mahesh Radadiya
answered Nov 30 '-1 00:00


used $index and it should work .

<input ng-model="cartdetail[$index]"></input>

revise full code :

<body  ng-app >
<div ng-init="cartdetail= ['1', '2', '3']">
{{cartdetail}}
<ul><li ng-repeat="num in cartdetail">
       <input ng-model="cartdetail[$index]"></input>
    </li>
</ul>

</div>
</body>
binding happen but input loses focus on each change - debugger  
Oct 4 '16 13:50
Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00

hope this work without loosing focus

<li ng-repeat="(j, value) in cartdetail track by $index">
       <input ng-model="cartdetail[j]"></input>
    </li>
shyam

shyam
answered Nov 30 '-1 00:00

use objects instead of primitives(e.g., number, string, boolean) to bind , so above code will be like

<body  ng-app >
<div ng-init="cartdetail= [{qty:'1'},{qty: '2'},{qty: '3'}]">
{{cartdetail}}
<ul><li ng-repeat="obj in cartdetail">
       <input ng-model="obj.qty"></input>
    </li>
</ul>
</div>
</body>


hope it work
Post Answer