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

Commit e0cf496

Browse files
lgalfasopetebacondarwin
authored andcommitted
fix($rootScope): don't clear phase if $apply is re-entered
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
1 parent a5221f3 commit e0cf496

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/ng/rootScope.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1040,11 +1040,14 @@ function $RootScopeProvider() {
10401040
$apply: function(expr) {
10411041
try {
10421042
beginPhase('$apply');
1043-
return this.$eval(expr);
1043+
try {
1044+
return this.$eval(expr);
1045+
} finally {
1046+
clearPhase();
1047+
}
10441048
} catch (e) {
10451049
$exceptionHandler(e);
10461050
} finally {
1047-
clearPhase();
10481051
try {
10491052
$rootScope.$digest();
10501053
} catch (e) {

test/ng/rootScopeSpec.js

+16
Original file line numberDiff line numberDiff line change
@@ -1505,6 +1505,22 @@ describe('Scope', function() {
15051505
}));
15061506

15071507

1508+
it('should not clear the state when calling $apply during an $apply', inject(
1509+
function($rootScope) {
1510+
$rootScope.$apply(function() {
1511+
expect(function() {
1512+
$rootScope.$apply();
1513+
}).toThrowMinErr('$rootScope', 'inprog', '$apply already in progress');
1514+
expect(function() {
1515+
$rootScope.$apply();
1516+
}).toThrowMinErr('$rootScope', 'inprog', '$apply already in progress');
1517+
});
1518+
expect(function() {
1519+
$rootScope.$apply();
1520+
}).not.toThrow();
1521+
}));
1522+
1523+
15081524
it('should throw an exception if $apply is called while flushing evalAsync queue', inject(
15091525
function($rootScope) {
15101526
expect(function() {

0 commit comments

Comments
 (0)