fix(ngEventDirs): check scope.$$phase only on $rootScope

Closes #8891, #8849
This commit is contained in:
Shahar Talmi
2014-08-29 23:31:37 +03:00
committed by Tobias Bosch
parent 36e6de1d91
commit 2712c2f197
2 changed files with 46 additions and 22 deletions

View File

@@ -49,7 +49,7 @@ forEach(
'click dblclick mousedown mouseup mouseover mouseout mousemove mouseenter mouseleave keydown keyup keypress submit focus blur copy cut paste'.split(' '),
function(name) {
var directiveName = directiveNormalize('ng-' + name);
ngEventDirectives[directiveName] = ['$parse', function($parse) {
ngEventDirectives[directiveName] = ['$parse', '$rootScope', function($parse, $rootScope) {
return {
compile: function($element, attr) {
var fn = $parse(attr[directiveName]);
@@ -59,7 +59,7 @@ forEach(
var callback = function() {
fn(scope, {$event:event});
};
if (forceAsyncEvents[eventName] && scope.$$phase) {
if (forceAsyncEvents[eventName] && $rootScope.$$phase) {
scope.$evalAsync(callback);
} else {
scope.$apply(callback);

View File

@@ -42,18 +42,30 @@ describe('event directives', function() {
describe('focus', function() {
it('should call the listener asynchronously during $apply',
inject(function($rootScope, $compile) {
element = $compile('<input type="text" ng-focus="focus()">')($rootScope);
$rootScope.focus = jasmine.createSpy('focus');
describe('call the listener asynchronously during $apply', function() {
function run(scope) {
inject(function($compile) {
element = $compile('<input type="text" ng-focus="focus()">')(scope);
scope.focus = jasmine.createSpy('focus');
$rootScope.$apply(function() {
element.triggerHandler('focus');
expect($rootScope.focus).not.toHaveBeenCalled();
});
scope.$apply(function() {
element.triggerHandler('focus');
expect(scope.focus).not.toHaveBeenCalled();
});
expect($rootScope.focus).toHaveBeenCalledOnce();
}));
expect(scope.focus).toHaveBeenCalledOnce();
});
}
it('should call the listener with non isolate scopes', inject(function($rootScope) {
run($rootScope.$new());
}));
it('should call the listener with isolate scopes', inject(function($rootScope) {
run($rootScope.$new(true));
}));
});
it('should call the listener synchronously inside of $apply if outside of $apply',
inject(function($rootScope, $compile) {
@@ -72,18 +84,30 @@ describe('event directives', function() {
describe('blur', function() {
it('should call the listener asynchronously during $apply',
inject(function($rootScope, $compile) {
element = $compile('<input type="text" ng-blur="blur()">')($rootScope);
$rootScope.blur = jasmine.createSpy('blur');
describe('call the listener asynchronously during $apply', function() {
function run(scope) {
inject(function($compile) {
element = $compile('<input type="text" ng-blur="blur()">')(scope);
scope.blur = jasmine.createSpy('blur');
$rootScope.$apply(function() {
element.triggerHandler('blur');
expect($rootScope.blur).not.toHaveBeenCalled();
});
scope.$apply(function() {
element.triggerHandler('blur');
expect(scope.blur).not.toHaveBeenCalled();
});
expect($rootScope.blur).toHaveBeenCalledOnce();
}));
expect(scope.blur).toHaveBeenCalledOnce();
});
}
it('should call the listener with non isolate scopes', inject(function($rootScope) {
run($rootScope.$new());
}));
it('should call the listener with isolate scopes', inject(function($rootScope) {
run($rootScope.$new(true));
}));
});
it('should call the listener synchronously inside of $apply if outside of $apply',
inject(function($rootScope, $compile) {