var myApp = angular.module('myApp', []); myApp.directive('myKeypressEnter', [function () { return function (scope, element, attrs) { element.on('keypress', function (event) { if (event.keyCode === 13) { scope.$apply(function () { scope.$eval(attrs.myKeypressEnter); }); } }); }; }]); myApp.controller('fooCtrl', ['$scope', function ($scope) { $scope.enter = function (str) { alert(str + ' ' + $scope.input); }; }]);
<div ng-app="myApp" ng-controller="fooCtrl"> <input type="text" my-keypress-enter="enter('Your input is')" ng-model="input" placeholder="press enter..." /> </div>