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

Commit 924e68c

Browse files
committed
fix(ngAria): trigger digest on ng-click via keypress, pass $event to expression
Minor improvement to ng-click directive from ngAria. Now, if bindings are updated during the click handler, the DOM will be updated as well. Additionally, the $event object is passed in to the expression via locals, as is done for core event directives. Closes #10442 Closes #10443 Closes #10447
1 parent f297aa5 commit 924e68c

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

src/ngAria/aria.js

+19-12
Original file line numberDiff line numberDiff line change
@@ -297,21 +297,28 @@ ngAriaModule.directive('ngShow', ['$aria', function($aria) {
297297
}
298298
};
299299
})
300-
.directive('ngClick',['$aria', function($aria) {
300+
.directive('ngClick',['$aria', '$parse', function($aria, $parse) {
301301
return {
302302
restrict: 'A',
303-
link: function(scope, elem, attr) {
304-
if ($aria.config('tabindex') && !elem.attr('tabindex')) {
305-
elem.attr('tabindex', 0);
306-
}
303+
compile: function(elem, attr) {
304+
var fn = $parse(attr.ngClick, /* interceptorFn */ null, /* expensiveChecks */ true);
305+
return function(scope, elem, attr) {
306+
if ($aria.config('tabindex') && !elem.attr('tabindex')) {
307+
elem.attr('tabindex', 0);
308+
}
307309

308-
if ($aria.config('bindKeypress') && !elem.attr('ng-keypress')) {
309-
elem.on('keypress', function(event) {
310-
if (event.keyCode === 32 || event.keyCode === 13) {
311-
scope.$eval(attr.ngClick);
312-
}
313-
});
314-
}
310+
if ($aria.config('bindKeypress') && !attr.ngKeypress) {
311+
elem.on('keypress', function(event) {
312+
if (event.keyCode === 32 || event.keyCode === 13) {
313+
scope.$apply(callback);
314+
}
315+
316+
function callback() {
317+
fn(scope, { $event: event });
318+
}
319+
});
320+
}
321+
};
315322
}
316323
};
317324
}])

test/ngAria/ariaSpec.js

+17
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,23 @@ describe('$aria', function() {
509509
expect(clickFn).not.toHaveBeenCalled();
510510
expect(keypressFn).toHaveBeenCalled();
511511
});
512+
513+
it('should update bindings when keypress handled', function() {
514+
compileInput('<div ng-click="text = \'clicked!\'">{{text}}</div>');
515+
expect(element.text()).toBe('');
516+
spyOn(scope.$root, '$digest').andCallThrough();
517+
element.triggerHandler({ type: 'keypress', keyCode: 13 });
518+
expect(element.text()).toBe('clicked!');
519+
expect(scope.$root.$digest).toHaveBeenCalledOnce();
520+
});
521+
522+
it('should pass $event to ng-click handler as local', function() {
523+
compileInput('<div ng-click="event = $event">{{event.type}}' +
524+
'{{event.keyCode}}</div>');
525+
expect(element.text()).toBe('');
526+
element.triggerHandler({ type: 'keypress', keyCode: 13 });
527+
expect(element.text()).toBe('keypress13');
528+
});
512529
});
513530

514531
describe('actions when bindKeypress set to false', function() {

0 commit comments

Comments
 (0)