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

feat($rootScope): allow passing locals argument to $evalAsync #10390

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 4 additions & 3 deletions src/ng/rootScope.js
Original file line number Diff line number Diff line change
@@ -743,7 +743,7 @@ function $RootScopeProvider() {
while (asyncQueue.length) {
try {
asyncTask = asyncQueue.shift();
asyncTask.scope.$eval(asyncTask.expression);
asyncTask.scope.$eval(asyncTask.expression, asyncTask.locals);
} catch (e) {
$exceptionHandler(e);
}
@@ -958,8 +958,9 @@ function $RootScopeProvider() {
* - `string`: execute using the rules as defined in {@link guide/expression expression}.
* - `function(scope)`: execute the function with the current `scope` parameter.
*
* @param {(object)=} locals Local variables object, useful for overriding values in scope.
*/
$evalAsync: function(expr) {
$evalAsync: function(expr, locals) {
// if we are outside of an $digest loop and this is the first time we are scheduling async
// task also schedule async auto-flush
if (!$rootScope.$$phase && !asyncQueue.length) {
@@ -970,7 +971,7 @@ function $RootScopeProvider() {
});
}

asyncQueue.push({scope: this, expression: expr});
asyncQueue.push({scope: this, expression: expr, locals: locals});
},

$$postDigest: function(fn) {
7 changes: 7 additions & 0 deletions test/ng/rootScopeSpec.js
Original file line number Diff line number Diff line change
@@ -1243,6 +1243,13 @@ describe('Scope', function() {
expect($rootScope.log).toBe('12');
}));

it('should allow passing locals to the expression', inject(function($rootScope) {
$rootScope.log = '';
$rootScope.$evalAsync('log = log + a', {a: 1});
$rootScope.$digest();
expect($rootScope.log).toBe('1');
}));

it('should run async expressions in their proper context', inject(function($rootScope) {
var child = $rootScope.$new();
$rootScope.ctx = 'root context';