From 1e8a944e70fe59cec06fea426ab8a2f7079f8d49 Mon Sep 17 00:00:00 2001 From: Martin Staffa Date: Tue, 15 Jul 2014 21:24:00 +0200 Subject: [PATCH] fix(ngEventDirs): check $$phase in handler to prevent inprog errors Fixes #5945 Closes #5402 --- src/ng/directive/ngEventDirs.js | 10 +++++--- test/ng/directive/ngEventDirsSpec.js | 34 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/ng/directive/ngEventDirs.js b/src/ng/directive/ngEventDirs.js index ed8ddd6ba167..10d1a20758e0 100644 --- a/src/ng/directive/ngEventDirs.js +++ b/src/ng/directive/ngEventDirs.js @@ -31,7 +31,7 @@ */ /* - * 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. @@ -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}); + }); + } }); }; } diff --git a/test/ng/directive/ngEventDirsSpec.js b/test/ng/directive/ngEventDirsSpec.js index 5b73c2dd6a8b..bb18d56dbd68 100644 --- a/test/ng/directive/ngEventDirsSpec.js +++ b/test/ng/directive/ngEventDirsSpec.js @@ -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('')($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('
' + + '
')($rootScope); + + $rootScope.$digest(); + expect($rootScope.focused).not.toBeDefined(); + + browserTrigger(element.children()[1], 'click'); + expect($rootScope.focused).toEqual(true); + })); + }); + });