Skip to content

Instantly share code, notes, and snippets.

@singhmohancs
Created May 27, 2016 07:51
Show Gist options
  • Save singhmohancs/317854a859098bffe9477f59eac8d915 to your computer and use it in GitHub Desktop.
Save singhmohancs/317854a859098bffe9477f59eac8d915 to your computer and use it in GitHub Desktop.
input enter
/**
* @ngDoc Directive
* @name app.Directive.onKeyEnter
* @module app
*
* @description
* this directive triggers callback function when enter key is pressed
*
* @author Mohan Singh <mslogicmaster@gmail.com>
*
*/
angular
.module('app', []);
angular
.module('app')
.directive('onKeyEnter', ['$parse', function($parse) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('keydown keypress', function(event) {
if (event.which === 13) {
var attrValue = $parse(attrs.onKeyEnter);
(typeof attrValue === 'function') ? attrValue(scope) : angular.noop();
event.preventDefault();
}
});
scope.$on('$destroy', function() {
element.unbind('keydown keypress')
})
}
};
}]);
@singhmohancs
Copy link
Author

@zorlac
Binding does work perfectly. if (event.which === 13) { does trigger angular Expression when ENTER key is pressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment