Skip to content
Permalink
Browse files

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
  • Loading branch information
caitp committed Dec 12, 2014
1 parent f297aa5 commit 924e68c7d522a1086969f3583d0ce87e59110bc5
Showing with 36 additions and 12 deletions.
  1. +19 −12 src/ngAria/aria.js
  2. +17 −0 test/ngAria/ariaSpec.js
@@ -297,21 +297,28 @@ ngAriaModule.directive('ngShow', ['$aria', function($aria) {
}
};
})
.directive('ngClick',['$aria', function($aria) {
.directive('ngClick',['$aria', '$parse', function($aria, $parse) {
return {
restrict: 'A',
link: function(scope, elem, attr) {
if ($aria.config('tabindex') && !elem.attr('tabindex')) {
elem.attr('tabindex', 0);
}
compile: function(elem, attr) {
var fn = $parse(attr.ngClick, /* interceptorFn */ null, /* expensiveChecks */ true);
return function(scope, elem, attr) {
if ($aria.config('tabindex') && !elem.attr('tabindex')) {
elem.attr('tabindex', 0);
}

if ($aria.config('bindKeypress') && !elem.attr('ng-keypress')) {
elem.on('keypress', function(event) {
if (event.keyCode === 32 || event.keyCode === 13) {
scope.$eval(attr.ngClick);
}
});
}
if ($aria.config('bindKeypress') && !attr.ngKeypress) {
elem.on('keypress', function(event) {
if (event.keyCode === 32 || event.keyCode === 13) {
scope.$apply(callback);
}

function callback() {
fn(scope, { $event: event });
}
});
}
};
}
};
}])
@@ -509,6 +509,23 @@ describe('$aria', function() {
expect(clickFn).not.toHaveBeenCalled();
expect(keypressFn).toHaveBeenCalled();
});

it('should update bindings when keypress handled', function() {
compileInput('<div ng-click="text = \'clicked!\'">{{text}}</div>');
expect(element.text()).toBe('');
spyOn(scope.$root, '$digest').andCallThrough();
element.triggerHandler({ type: 'keypress', keyCode: 13 });
expect(element.text()).toBe('clicked!');
expect(scope.$root.$digest).toHaveBeenCalledOnce();
});

it('should pass $event to ng-click handler as local', function() {
compileInput('<div ng-click="event = $event">{{event.type}}' +
'{{event.keyCode}}</div>');
expect(element.text()).toBe('');
element.triggerHandler({ type: 'keypress', keyCode: 13 });
expect(element.text()).toBe('keypress13');
});
});

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

0 comments on commit 924e68c

Please sign in to comment.
You can’t perform that action at this time.