BootDB

Code | Open Source | Web

How to Trigger Event on Pressing Enter Key in AngularJS

Use the below directive which triggers a function when enter / return key is pressed:

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

Usage:

<input ng-model="input" ng-enter="enterTriggered">