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

Commit

Permalink
fix($rootScope): don't clear phase if $apply is re-entered
Browse files Browse the repository at this point in the history
We cannot re-enter a `$apply` block while already in a `$apply` or `$digest`
phase.

Before this change such an invalid call to `$apply` was incorrectly clearing
the phase, which meant that a second invalid `$apply` call was being allowed.

Closes #12174
  • Loading branch information
lgalfaso authored and petebacondarwin committed Jul 25, 2015
1 parent a5221f3 commit e0cf496
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/ng/rootScope.js
Expand Up @@ -1040,11 +1040,14 @@ function $RootScopeProvider() {
$apply: function(expr) {
try {
beginPhase('$apply');
return this.$eval(expr);
try {
return this.$eval(expr);
} finally {
clearPhase();
}
} catch (e) {
$exceptionHandler(e);
} finally {
clearPhase();
try {
$rootScope.$digest();
} catch (e) {
Expand Down
16 changes: 16 additions & 0 deletions test/ng/rootScopeSpec.js
Expand Up @@ -1505,6 +1505,22 @@ describe('Scope', function() {
}));


it('should not clear the state when calling $apply during an $apply', inject(
function($rootScope) {
$rootScope.$apply(function() {
expect(function() {
$rootScope.$apply();
}).toThrowMinErr('$rootScope', 'inprog', '$apply already in progress');
expect(function() {
$rootScope.$apply();
}).toThrowMinErr('$rootScope', 'inprog', '$apply already in progress');
});
expect(function() {
$rootScope.$apply();
}).not.toThrow();
}));


it('should throw an exception if $apply is called while flushing evalAsync queue', inject(
function($rootScope) {
expect(function() {
Expand Down

0 comments on commit e0cf496

Please sign in to comment.