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

Commit 10cc1a4

Browse files
lgalfasobtford
authored andcommitted
fix($scope): $evalAsync executes on the right scope
Executes $evalAsync at the scope that the call was made Closes: #3548
1 parent 4041482 commit 10cc1a4

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/ng/rootScope.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ function $RootScopeProvider(){
501501
dirty, ttl = TTL,
502502
next, current, target = this,
503503
watchLog = [],
504-
logIdx, logMsg;
504+
logIdx, logMsg, asyncTask;
505505

506506
beginPhase('$digest');
507507

@@ -511,7 +511,8 @@ function $RootScopeProvider(){
511511

512512
while(asyncQueue.length) {
513513
try {
514-
current.$eval(asyncQueue.shift());
514+
asyncTask = asyncQueue.shift();
515+
asyncTask.scope.$eval(asyncTask.expression);
515516
} catch (e) {
516517
$exceptionHandler(e);
517518
}
@@ -704,7 +705,7 @@ function $RootScopeProvider(){
704705
});
705706
}
706707

707-
this.$$asyncQueue.push(expr);
708+
this.$$asyncQueue.push({scope: this, expression: expr});
708709
},
709710

710711
$$postDigest : function(fn) {

test/ng/rootScopeSpec.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,17 @@ describe('Scope', function() {
766766
expect($rootScope.log).toBe('12');
767767
}));
768768

769+
it('should run async expressions in their proper context', inject(function ($rootScope) {
770+
var child = $rootScope.$new();
771+
$rootScope.ctx = 'root context';
772+
$rootScope.log = '';
773+
child.ctx = 'child context';
774+
child.log = '';
775+
child.$evalAsync('log=ctx');
776+
$rootScope.$digest();
777+
expect($rootScope.log).toBe('');
778+
expect(child.log).toBe('child context');
779+
}));
769780

770781
it('should operate only with a single queue across all child and isolate scopes', inject(function($rootScope) {
771782
var childScope = $rootScope.$new();
@@ -777,7 +788,10 @@ describe('Scope', function() {
777788

778789
expect(childScope.$$asyncQueue).toBe($rootScope.$$asyncQueue);
779790
expect(isolateScope.$$asyncQueue).toBe($rootScope.$$asyncQueue);
780-
expect($rootScope.$$asyncQueue).toEqual(['rootExpression', 'childExpression', 'isolateExpression']);
791+
expect($rootScope.$$asyncQueue).toEqual([
792+
{scope: $rootScope, expression: 'rootExpression'},
793+
{scope: childScope, expression: 'childExpression'},
794+
{scope: isolateScope, expression: 'isolateExpression'}]);
781795
}));
782796

783797

0 commit comments

Comments
 (0)