Skip to content

Commit 1e8a944

Browse files
committed
fix(ngEventDirs): check $$phase in handler to prevent inprog errors
Fixes angular#5945 Closes angular#5402
1 parent 840e889 commit 1e8a944

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/ng/directive/ngEventDirs.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
</example>
3232
*/
3333
/*
34-
* A directive that allows creation of custom onclick handlers that are defined as angular
34+
* A directive that allows creation of custom event handlers that are defined as angular
3535
* expressions and are compiled and executed within the current scope.
3636
*
3737
* Events that are handled via these handler are always configured not to propagate further.
@@ -47,9 +47,13 @@ forEach(
4747
var fn = $parse(attr[directiveName]);
4848
return function ngEventHandler(scope, element) {
4949
element.on(lowercase(name), function(event) {
50-
scope.$apply(function() {
50+
if (scope.$$phase) {
5151
fn(scope, {$event:event});
52-
});
52+
} else {
53+
scope.$apply(function() {
54+
fn(scope, {$event:event});
55+
});
56+
}
5357
});
5458
};
5559
}

test/ng/directive/ngEventDirsSpec.js

+34
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,38 @@ describe('event directives', function() {
3939
expect($rootScope.formSubmitted).toEqual('foo');
4040
}));
4141
});
42+
43+
describe('ngBlur', function() {
44+
it('should get called when ngKeydown triggers blur', inject(function($rootScope, $compile) {
45+
$rootScope.blur = function() {
46+
browserTrigger(element, 'blur');
47+
};
48+
49+
element = $compile('<input type="text" ng-blur="blurred = true" ng-keydown="blur()" />')($rootScope);
50+
51+
$rootScope.$digest();
52+
expect($rootScope.blurred).not.toBeDefined();
53+
54+
browserTrigger(element, 'keydown');
55+
expect($rootScope.blurred).toEqual(true);
56+
}));
57+
});
58+
59+
describe('ngFocus', function() {
60+
it('should get called when ngClick triggers focus', inject(function($rootScope, $compile) {
61+
$rootScope.focus = function() {
62+
browserTrigger(element.children()[0], 'focus');
63+
};
64+
65+
element = $compile('<div><input type="text" ng-focus="focused = true" />' +
66+
'<button type="button" ng-click="focus()"></button></div>')($rootScope);
67+
68+
$rootScope.$digest();
69+
expect($rootScope.focused).not.toBeDefined();
70+
71+
browserTrigger(element.children()[1], 'click');
72+
expect($rootScope.focused).toEqual(true);
73+
}));
74+
});
75+
4276
});

0 commit comments

Comments
 (0)