Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit f20646b

Browse files
marknadigpkozlowski-opensource
authored andcommitted
feat(directive): add ngKeypress directive for handling keypress event
1 parent 65e57a7 commit f20646b

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/ng/directive/ngEventDirs.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*/
3838
var ngEventDirectives = {};
3939
forEach(
40-
'click dblclick mousedown mouseup mouseover mouseout mousemove mouseenter mouseleave keydown keyup'.split(' '),
40+
'click dblclick mousedown mouseup mouseover mouseout mousemove mouseenter mouseleave keydown keyup keypress'.split(' '),
4141
function(name) {
4242
var directiveName = directiveNormalize('ng-' + name);
4343
ngEventDirectives[directiveName] = ['$parse', function($parse) {
@@ -196,6 +196,22 @@ forEach(
196196
*/
197197

198198

199+
/**
200+
* @ngdoc directive
201+
* @name ng.directive:ngKeypress
202+
*
203+
* @description
204+
* Specify custom behavior on keypress event.
205+
*
206+
* @element ANY
207+
* @param {expression} ngKeypress {@link guide/expression Expression} to evaluate upon
208+
* keypress. (Event object is available as `$event` and can be interrogated for keyCode, altKey, etc.)
209+
*
210+
* @example
211+
* See {@link ng.directive:ngClick ngClick}
212+
*/
213+
214+
199215
/**
200216
* @ngdoc directive
201217
* @name ng.directive:ngSubmit

test/ng/directive/ngKeySpec.js

+9
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,14 @@ describe('ngKeyup and ngKeydown directives', function() {
2525
expect($rootScope.touched).toEqual(true);
2626
}));
2727

28+
it('should get called on a keypress', inject(function($rootScope, $compile) {
29+
element = $compile('<input ng-keypress="touched = true">')($rootScope);
30+
$rootScope.$digest();
31+
expect($rootScope.touched).toBeFalsy();
32+
33+
browserTrigger(element, 'keypress');
34+
expect($rootScope.touched).toEqual(true);
35+
}));
36+
2837
});
2938

0 commit comments

Comments
 (0)