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

fix(ngEventDirs): blur and focus use $evalAsync to prevent inprog errors #6910

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/ng/directive/ngEventDirs.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</example>
*/
/*
* A directive that allows creation of custom onclick handlers that are defined as angular
* A directive that allows creation of custom event handlers that are defined as angular
* expressions and are compiled and executed within the current scope.
*
* Events that are handled via these handler are always configured not to propagate further.
Expand All @@ -47,9 +47,13 @@ forEach(
var fn = $parse(attr[directiveName]);
return function ngEventHandler(scope, element) {
element.on(lowercase(name), function(event) {
scope.$apply(function() {
if (scope.$$phase) {
fn(scope, {$event:event});
});
} else {
scope.$apply(function() {
fn(scope, {$event:event});
});
}
});
};
}
Expand Down
34 changes: 34 additions & 0 deletions test/ng/directive/ngEventDirsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,38 @@ describe('event directives', function() {
expect($rootScope.formSubmitted).toEqual('foo');
}));
});

describe('ngBlur', function() {
it('should get called when ngKeydown triggers blur', inject(function($rootScope, $compile) {
$rootScope.blur = function() {
browserTrigger(element, 'blur');
};

element = $compile('<input type="text" ng-blur="blurred = true" ng-keydown="blur()" />')($rootScope);

$rootScope.$digest();
expect($rootScope.blurred).not.toBeDefined();

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

describe('ngFocus', function() {
it('should get called when ngClick triggers focus', inject(function($rootScope, $compile) {
$rootScope.focus = function() {
browserTrigger(element.children()[0], 'focus');
};

element = $compile('<div><input type="text" ng-focus="focused = true" />' +
'<button type="button" ng-click="focus()"></button></div>')($rootScope);

$rootScope.$digest();
expect($rootScope.focused).not.toBeDefined();

browserTrigger(element.children()[1], 'click');
expect($rootScope.focused).toEqual(true);
}));
});

});