2

この例では、入力ごとに複数の文字を入力できない理由を知りたいです。

http://jsfiddle.net/R3uY4/2/

<div ng-app>
  <div ng-controller="ctrl">
     <table>
            <tr>
                <td ng-repeat="f in foo">
                    <input ng-model="foo[$index]" style="width:60px" ></input>
                </td>
            </tr>
        </table>
  </div>
</div>

js:

function ctrl($scope) {
  $scope.foo = [];
    for (var i=0; i<5; i++) {
        $scope.foo.push('');
    }  
}
4

2 に答える 2

4

モデルをプリミティブにバインドしています。それはそのようには機能しません。このgithubの問題の完全な説明。また、入力値を変更するたびにng-repeatが更新されます。それがあなたが焦点を失っている理由です。

常にオブジェクトにバインドします。

HTML:

<td ng-repeat="f in foo">
  <input ng-model="f.value" style="width:60px" />
</td>

コントローラ:

function ctrl($scope) {
  $scope.foo = [];
    for (var i=0; i<5; i++) {
        $scope.foo.push({value: ''});
    }
}
于 2013-03-12T21:58:39.867 に答える
1

HTML5 の使用を気にしない場合は、autofocus属性を使用できます。inputフィールドに追加するだけです。

<li ng-repeat="i in items">{{i.name}} <input ng-model="i.description" autofocus></li>

これが jsFiddleのフォークです。

于 2013-03-12T21:52:53.927 に答える