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

Phasecheck #8891

Closed
wants to merge 2 commits 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
2 changes: 1 addition & 1 deletion src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) {
// a row.
var revalidate = validity && ctrl.$$hasNativeValidators;
if (ctrl.$viewValue !== value || (value === '' && revalidate)) {
if (scope.$$phase) {
if (scope.$root.$$phase) {
ctrl.$setViewValue(value, event, revalidate);
} else {
scope.$apply(function() {
Expand Down
4 changes: 2 additions & 2 deletions src/ng/directive/ngEventDirs.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,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 {
restrict: 'A',
compile: function($element, attr) {
Expand All @@ -62,7 +62,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);
Expand Down
31 changes: 21 additions & 10 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1152,19 +1152,30 @@ describe('input', function() {
});

if (!_jqLiteMode) {
it('should not cause the double $digest when triggering an event using jQuery', function() {
$sniffer.hasEvent = function(eventName) {
return eventName !== 'input';
};
describe('double $digest when triggering an event using jQuery', function() {
function run() {
$sniffer.hasEvent = function(eventName) {
return eventName !== 'input';
};

compileInput('<input type="text" ng-model="name" name="alias" ng-change="change()" />');

scope.field = 'fake field';
scope.$watch('field', function() {
// We need to use _originalTrigger since trigger is modified by Angular Scenario.
inputElm._originalTrigger('change');
});
scope.$apply();
}

compileInput('<input type="text" ng-model="name" name="alias" ng-change="change()" />');
it('should not cause the double $digest with non isolate scopes', function() {
run();
});

scope.field = 'fake field';
scope.$watch('field', function() {
// We need to use _originalTrigger since trigger is modified by Angular Scenario.
inputElm._originalTrigger('change');
it('should not cause the double $digest with isolate scopes', function() {
scope = scope.$new(true);
run();
});
scope.$apply();
});
}
});
Expand Down
64 changes: 44 additions & 20 deletions test/ng/directive/ngEventDirsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down