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

Commit 192ff61

Browse files
committed
feat(scope.$eval): Allow passing locals to the expression
1 parent 935c101 commit 192ff61

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

Diff for: src/service/scope.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -503,12 +503,13 @@ function $RootScopeProvider(){
503503
* @param {(string|function())=} expression An angular expression to be executed.
504504
*
505505
* - `string`: execute using the rules as defined in {@link guide/dev_guide.expressions expression}.
506-
* - `function(scope)`: execute the function with the current `scope` parameter.
506+
* - `function(scope, locals)`: execute the function with the current `scope` parameter.
507+
* @param {Object=} locals Hash object of local variables for the expression.
507508
*
508509
* @returns {*} The result of evaluating the expression.
509510
*/
510-
$eval: function(expr) {
511-
return $parse(expr)(this);
511+
$eval: function(expr, locals) {
512+
return $parse(expr)(this, locals);
512513
},
513514

514515
/**

Diff for: test/service/scopeSpec.js

+11
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,19 @@ describe('Scope', function() {
416416
$rootScope.$eval(function(self) {self.b=2;});
417417
expect($rootScope.b).toEqual(2);
418418
}));
419+
420+
421+
it('should allow passing locals to the expression', inject(function($rootScope) {
422+
expect($rootScope.$eval('a+1', {a: 2})).toBe(3);
423+
424+
$rootScope.$eval(function(scope, locals) {
425+
scope.c = locals.b + 4;
426+
}, {b: 3});
427+
expect($rootScope.c).toBe(7);
428+
}));
419429
});
420430

431+
421432
describe('$evalAsync', function() {
422433

423434
it('should run callback before $watch', inject(function($rootScope) {

0 commit comments

Comments
 (0)