Linked input is not focused in angularjs
I run this simple code with angularjs:
HTML:
<div ng-app ng-controller="AController">
<code>{{ itemsInArray }}</code>
<div ng-repeat="item in itemsInArray">
<input ng-model="itemsInArray[$index]" />
</div>
</div>
JavaScript:
function AController($scope) {
$scope.itemsInArray = ["strA", "strB", "strC"];
}
Binding seems to work correctly when indexing into an array, but after entering one character, the input loses focus.
Here you can find the working code on this script: http://jsfiddle.net/QygW8/
+3
3 answers
I think this is because you are manipulating the same element that repeats over ng-repeat. Thus, ng-repeat sees the change in the element and re-runs `ng-repeat, which regenerates the elements.
If you look at your html script, you may notice this effect.
To make it work, one way to do it
http://jsfiddle.net/cmyworld/CvLBS/
$scope.itemsInArray = [{data:"strA"}, {data:"strB"}, {data:"strC"}];
item.data
+3