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

Commit

Permalink
feat(directive): add ngKeypress directive for handling keypress event
Browse files Browse the repository at this point in the history
  • Loading branch information
marknadig authored and pkozlowski-opensource committed Mar 8, 2013
1 parent 65e57a7 commit f20646b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/ng/directive/ngEventDirs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions test/ng/directive/ngKeySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<input ng-keypress="touched = true">')($rootScope);
$rootScope.$digest();
expect($rootScope.touched).toBeFalsy();

browserTrigger(element, 'keypress');
expect($rootScope.touched).toEqual(true);
}));

});

0 comments on commit f20646b

Please sign in to comment.