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

Commit

Permalink
feat(directive): ng:keydown, ng:keyup
Browse files Browse the repository at this point in the history
New directives for binding to keydown and keyup events.

Closes #1035
  • Loading branch information
marknadig authored and pkozlowski-opensource committed Dec 18, 2012
1 parent f2d5261 commit e03182f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
34 changes: 33 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'.split(' '),
'click dblclick mousedown mouseup mouseover mouseout mousemove mouseenter mouseleave keydown keyup'.split(' '),
function(name) {
var directiveName = directiveNormalize('ng-' + name);
ngEventDirectives[directiveName] = ['$parse', function($parse) {
Expand Down Expand Up @@ -164,6 +164,38 @@ forEach(
*/


/**
* @ngdoc directive
* @name ng.directive:ngKeydown
*
* @description
* Specify custom behavior on keydown event.
*
* @element ANY
* @param {expression} ngKeydown {@link guide/expression Expression} to evaluate upon
* keydown. (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:ngKeyup
*
* @description
* Specify custom behavior on keyup event.
*
* @element ANY
* @param {expression} ngKeyup {@link guide/expression Expression} to evaluate upon
* keyup. (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
29 changes: 29 additions & 0 deletions test/ng/directive/ngKeySpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

describe('ngKeyup and ngKeydown directives', function() {
var element;

afterEach(function() {
dealoc(element);
});

it('should get called on a keyup', inject(function($rootScope, $compile) {
element = $compile('<input ng-keyup="touched = true">')($rootScope);
$rootScope.$digest();
expect($rootScope.touched).toBeFalsy();

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

it('should get called on a keydown', inject(function($rootScope, $compile) {
element = $compile('<input ng-keydown="touched = true">')($rootScope);
$rootScope.$digest();
expect($rootScope.touched).toBeFalsy();

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

});

0 comments on commit e03182f

Please sign in to comment.