AngularJS でキー押下イベントを使用するにはどうすればいいですか? 質問する

AngularJS でキー押下イベントを使用するにはどうすればいいですか? 質問する

下のテキスト ボックスで Enter キーの押下イベントをキャッチしたいです。より明確にするために、ng-repeattbody にデータを入力するには を使用します。HTML は次のとおりです。

<td><input type="number" id="closeqty{{$index}}" class="pagination-right closefield" 
    data-ng-model="closeqtymodel" data-ng-change="change($index)" required placeholder="{{item.closeMeasure}}" /></td>

これは私のモジュールです:

angular.module('components', ['ngResource']);

私はリソースを使用してテーブルにデータを入力し、コントローラー コードは次のとおりです。

function Ajaxy($scope, $resource) {
//controller which has resource to populate the table 
}

ベストアンサー1

次のようにを追加する必要がありますdirective

ジャバスクリプト:

app.directive('myEnter', function () {
    return function (scope, element, attrs) {
        element.bind("keydown keypress", function (event) {
            if(event.which === 13) {
                scope.$apply(function (){
                    scope.$eval(attrs.myEnter);
                });

                event.preventDefault();
            }
        });
    };
});

HTML :

<div ng-app="" ng-controller="MainCtrl">
    <input type="text" my-enter="doSomething()">    
</div>

おすすめ記事