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

Closes #10390
This commit is contained in:
Shahar Talmi
2014-12-09 23:43:13 +02:00
committed by Peter Bacon Darwin
parent c90ad96808
commit 9b96cea462
2 changed files with 11 additions and 3 deletions

View File

@@ -742,7 +742,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);
}
@@ -957,8 +957,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) {
@@ -969,7 +970,7 @@ function $RootScopeProvider() {
});
}
asyncQueue.push({scope: this, expression: expr});
asyncQueue.push({scope: this, expression: expr, locals: locals});
},
$$postDigest: function(fn) {

View File

@@ -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';