From f20646bce5f0c914992a78fc2556bda136c27ac9 Mon Sep 17 00:00:00 2001 From: Mark Nadig Date: Tue, 5 Mar 2013 10:43:06 -0700 Subject: [PATCH] feat(directive): add ngKeypress directive for handling keypress event --- src/ng/directive/ngEventDirs.js | 18 +++++++++++++++++- test/ng/directive/ngKeySpec.js | 9 +++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/ng/directive/ngEventDirs.js b/src/ng/directive/ngEventDirs.js index e2aedd7ff509..197db5ac4aa2 100644 --- a/src/ng/directive/ngEventDirs.js +++ b/src/ng/directive/ngEventDirs.js @@ -37,7 +37,7 @@ */ var ngEventDirectives = {}; forEach( - 'click dblclick mousedown mouseup mouseover mouseout mousemove mouseenter mouseleave keydown keyup'.split(' '), + 'click dblclick mousedown mouseup mouseover mouseout mousemove mouseenter mouseleave keydown keyup keypress'.split(' '), function(name) { var directiveName = directiveNormalize('ng-' + name); ngEventDirectives[directiveName] = ['$parse', function($parse) { @@ -196,6 +196,22 @@ forEach( */ +/** + * @ngdoc directive + * @name ng.directive:ngKeypress + * + * @description + * Specify custom behavior on keypress event. + * + * @element ANY + * @param {expression} ngKeypress {@link guide/expression Expression} to evaluate upon + * keypress. (Event object is available as `$event` and can be interrogated for keyCode, altKey, etc.) + * + * @example + * See {@link ng.directive:ngClick ngClick} + */ + + /** * @ngdoc directive * @name ng.directive:ngSubmit diff --git a/test/ng/directive/ngKeySpec.js b/test/ng/directive/ngKeySpec.js index d8c6f9dee46c..c7b989a48b14 100644 --- a/test/ng/directive/ngKeySpec.js +++ b/test/ng/directive/ngKeySpec.js @@ -25,5 +25,14 @@ describe('ngKeyup and ngKeydown directives', function() { expect($rootScope.touched).toEqual(true); })); + it('should get called on a keypress', inject(function($rootScope, $compile) { + element = $compile('')($rootScope); + $rootScope.$digest(); + expect($rootScope.touched).toBeFalsy(); + + browserTrigger(element, 'keypress'); + expect($rootScope.touched).toEqual(true); + })); + });