-
Notifications
You must be signed in to change notification settings - Fork 27.5k
<input> loses focus when ng-model references ng-repeat array directly #13327
Comments
I think there's a related issue floating around somewhere, but that was because the repeated elements were moved, which caused them to lose the focus. Since in this example, there should happen no DOM reordering, this needs an investigation. |
I think this is expected behaviour. As @Narretz mentioned, your are repeating over an array and you are changing the items of the array (note that your items are strings, which are primitives in JS and thus compared "by value"). Since new items are detected, old elements are removed from the DOM and new ones are created (which obviously don't get focus). There are several ways to achieve the desired behaviour:
You can see all 3 solutions in action in this demo pen. |
Thanks @gkalpak. You are right, the behavior is expected. |
I ended up using option #3. Thanks for clarifying the issue (in hindsight its obvious). It would help if there was some documentation on Angular 'gotcha' issues.. this one is a prime candidate for that list. |
TBH, I would expect you'd follow option #1. It's quite typical to use it like this - I wonder why can't you use it (if there's a specific reason). |
Option #1 (as coded in the codepen) is what I was using originally. It does not work (at least when I try it in the codepen) for the reasons you gave above... perhaps you meant something different in the codepen? Nevertheless, option #3 is most closely related to my code. The array that I |
If you stumbled upon this thread and are looking for a solution in Angular 2, the solution is quite simple. Example:
and update the value in onValueUpdate function. Since the change event triggers only when you blur from input, your will not face the error anymore. |
thanks @gkalpak |
I have a simple
<input>
inside a<div ng-repeat...">
As soon as I type a character the
<input>
focus is lost. To continue typing I first have to click inside the input area for each character I want to enter.The essence of the ng-repeat code that shows the problem is:
However, an
<input>
inside theng-repeat
scope but which does not directly reference theng-repeat
array, works fine - I can enter data all day long. For example:Note: the problem is clearly related to the the
ng-model
referring directly to the$index
-th element in the items array. My guess is that a $watch on the collection is triggered and that causes the loss of focus.Here's a
codepen
that shows the problem:http://codepen.io/djmarcus/pen/bVZGvj
Here's a distillation:
The text was updated successfully, but these errors were encountered: