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

Commit e03182f

Browse files
marknadigpkozlowski-opensource
authored andcommitted
feat(directive): ng:keydown, ng:keyup
New directives for binding to keydown and keyup events. Closes #1035
1 parent f2d5261 commit e03182f

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/ng/directive/ngEventDirs.js

+33-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'.split(' '),
40+
'click dblclick mousedown mouseup mouseover mouseout mousemove mouseenter mouseleave keydown keyup'.split(' '),
4141
function(name) {
4242
var directiveName = directiveNormalize('ng-' + name);
4343
ngEventDirectives[directiveName] = ['$parse', function($parse) {
@@ -164,6 +164,38 @@ forEach(
164164
*/
165165

166166

167+
/**
168+
* @ngdoc directive
169+
* @name ng.directive:ngKeydown
170+
*
171+
* @description
172+
* Specify custom behavior on keydown event.
173+
*
174+
* @element ANY
175+
* @param {expression} ngKeydown {@link guide/expression Expression} to evaluate upon
176+
* keydown. (Event object is available as `$event` and can be interrogated for keyCode, altKey, etc.)
177+
*
178+
* @example
179+
* See {@link ng.directive:ngClick ngClick}
180+
*/
181+
182+
183+
/**
184+
* @ngdoc directive
185+
* @name ng.directive:ngKeyup
186+
*
187+
* @description
188+
* Specify custom behavior on keyup event.
189+
*
190+
* @element ANY
191+
* @param {expression} ngKeyup {@link guide/expression Expression} to evaluate upon
192+
* keyup. (Event object is available as `$event` and can be interrogated for keyCode, altKey, etc.)
193+
*
194+
* @example
195+
* See {@link ng.directive:ngClick ngClick}
196+
*/
197+
198+
167199
/**
168200
* @ngdoc directive
169201
* @name ng.directive:ngSubmit

test/ng/directive/ngKeySpec.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
describe('ngKeyup and ngKeydown directives', function() {
4+
var element;
5+
6+
afterEach(function() {
7+
dealoc(element);
8+
});
9+
10+
it('should get called on a keyup', inject(function($rootScope, $compile) {
11+
element = $compile('<input ng-keyup="touched = true">')($rootScope);
12+
$rootScope.$digest();
13+
expect($rootScope.touched).toBeFalsy();
14+
15+
browserTrigger(element, 'keyup');
16+
expect($rootScope.touched).toEqual(true);
17+
}));
18+
19+
it('should get called on a keydown', inject(function($rootScope, $compile) {
20+
element = $compile('<input ng-keydown="touched = true">')($rootScope);
21+
$rootScope.$digest();
22+
expect($rootScope.touched).toBeFalsy();
23+
24+
browserTrigger(element, 'keydown');
25+
expect($rootScope.touched).toEqual(true);
26+
}));
27+
28+
});
29+

0 commit comments

Comments
 (0)