Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

<input> loses focus when ng-model references ng-repeat array directly #13327

Closed
@djmarcus1

Description

@djmarcus1

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:

<div ng-repeat="item in items">
    <input ng-model="items[$index]">
</div>

However, an <input> inside the ng-repeat scope but which does not directly reference the ng-repeat array, works fine - I can enter data all day long. For example:

<div ng-repeat="item in items">
    <input ng-model="george">
</div>

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:

    <div ng-controller="myCtrl">
        <div ng-repeat="item in items">
          <div>This fails</div>
          <div layout="row" layout-align="start center">
            <input ng-model="items[$index]"  label=""> 
          </div>
        </div>

        <div ng-repeat="item in items">
          <div>This works</div>
          <div layout="row" layout-align="start center">
            <input ng-model="george"  label=""> 
          </div>
        </div>

        <div>This works (its outside of ng-repeat) </div>
        <input ng-model="harvey" label="">
    </div>

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions